App crashed at textview.becomeFirstResponder

强颜欢笑 提交于 2019-12-11 14:31:05

问题


My app sometimes crashes at the call to textView.becomeFirstResponder(). The error thrown is strange:

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

Sometimes it's:

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

I did add notification listeners:

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

But the observer is the custom view I defined, why does the system send notification to UITextSelectionView or UIImageView?

Found in iOS 8.4.1, not reproduced in iOS 9.

What is happening here?


回答1:


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
}



回答2:


In 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)
}

or

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)
}


来源:https://stackoverflow.com/questions/44601357/app-crashed-at-textview-becomefirstresponder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!