在IB的库中,介绍告诉我们,当按下返回键时,UITextView的键盘将消失。但实际上返回键只能作为'\n'

我可以添加一个按钮,并使用[txtView resignFirstResponder]隐藏键盘。

但是有没有办法为键盘中的返回键添加动作,这样我就不需要添加UIButton了?


当前回答

银行的国际代码

在你的类/视图中实现UITextViewDelegate,如下所示:

class MyClass: UITextViewDelegate  { ...

设置textView委托为self

myTextView.delegate = self

然后实现以下内容:

  func textViewDidChange(_ textView: UITextView) {
    if textView.text.characters.count >= 1 {

        if let lastChar = textView.text.characters.last {

            if(lastChar == "\n"){

              textView.text = textView.text.substring(to: textView.text.index(before: textView.text.endIndex))
              textView.resignFirstResponder()
            }
        }
    }
}

编辑 我更新了代码,因为它从来都不是一个好主意,改变用户在文本框的输入,以解决问题,而不是在黑客代码完成后重新设置状态。

其他回答

类似于使用UITextViewDelegate的其他答案,但一个更新的swift接口isNewline将是:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if let character = text.first, character.isNewline {
        textView.resignFirstResponder()
        return false
    }
    return true
}

对于Swift 3,这段代码允许我在UITextView之外按下键来解除键盘。

@IBOutlet weak var comment: UITextView!

   override func viewDidLoad() {
        super.viewDidLoad()

        comment.delegate = self

        let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(tap))
        view.addGestureRecognizer(tapGestureRecogniser)
    }

    func tap(sender: UITapGestureRecognizer) {
        if comment.isFirstResponder {
            comment.resignFirstResponder()
        }
    }

在viewDidLoad中添加一个观察者

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil];

然后使用选择器检查"\n"

-(void) textViewKeyPressed: (NSNotification*) notification {

  if ([[[notification object] text] hasSuffix:@"\n"])
  {
    [[notification object] resignFirstResponder];
  }
}

它确实使用“\n”,而不是专门检查返回键,但我认为这是可以的。

更新

参见下面ribto的答案,它使用[NSCharacterSet newlineCharacterSet]来代替\n

使用导航控制器托管一个栏来解除键盘:

在.h文件中:

UIBarButtonItem* dismissKeyboardButton;

在.m文件中:

- (void)viewDidLoad {
    dismissKeyboardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard)];
}

-(void)textViewDidBeginEditing:(UITextView *)textView {
    self.navigationItem.rightBarButtonItem = dismissKeyboardButton;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    self.navigationItem.rightBarButtonItem = dismissKeyboardButton;
}

-(void)dismissKeyboard {
    [self.textField resignFirstResponder];
    [self.textView resignFirstResponder];
    //or replace this with your regular right button
    self.navigationItem.rightBarButtonItem = nil;
}

迅速回答:

override func viewDidLoad() {
    super.viewDidLoad()
    let tapGestureReconizer = UITapGestureRecognizer(target: self, action: "tap:")
    view.addGestureRecognizer(tapGestureReconizer)
}

func tap(sender: UITapGestureRecognizer) {
    view.endEditing(true)
}