应用程序在textview.becomeFirstResponder

时间:2017-06-17 06:03:44

标签: ios iphone swift keyboard-events

我的应用有时会在拨打textView.becomeFirstResponder()时崩溃。抛出的错误很奇怪:

-[UITextSelectionView keyboardShownWithNotif:]: unrecognized selector sent to instance 0x16899070

有时它是:

-[UIImageView keyboardShownWithNotif:]: unrecognized selector sent to instance 0x178e2610

我确实添加了通知监听器:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown(notif:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHidden), name: .UIKeyboardWillHide, object: nil)

但观察者是我定义的自定义视图,为什么系统会向UITextSelectionViewUIImageView发送通知?

在iOS 8.4.1中找到,未在iOS 9中重现。

这里发生了什么?

2 个答案:

答案 0 :(得分:2)

seems like you added an notif. observer to show/hide keyboard. 
Try to remove observer in dealloc method

 - (void) dealloc {
       [[NSNotificationCenter defaultCenter] removeObserver:self]; //Or whichever observer you want to remove
}

答案 1 :(得分:0)

在swift 3中:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}

deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}
相关问题