应用程序坠毁在textview.becomeFirstResponder
问题描述:
我的应用程序有时会崩溃在textView.becomeFirstResponder()
的电话。抛出的错误是奇怪:应用程序坠毁在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)
但观察者是我定义的自定义视图,为什么系统发送通知到UITextSelectionView
或UIImageView
?
在iOS 8.4.1中找到,未在iOS中复制9.
这里发生了什么?
答
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
}
答
在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)
}
你从哪里得到这个代码吗? –
你在textfiew/textfield的delegate方法中写了什么 – vaibby
我已经更新了这个问题。 – NeoWang