应用程序崩溃:异常 - 发送到实例的无法识别的选择器

App crashing: Exception - unrecognised selector sent to instance

我有一个 UITableviewController,它具有以下逻辑,一旦键盘像这样切换,就可以向上/向下滑动整个视图:

class ChatDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
 // variables...

 override func viewDidLoad() {
        super.viewDidLoad()
        // do stuff...
        NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillShow:")), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide:")), name: UIResponder.keyboardWillHideNotification, object: nil)
       // do other stuff...
}
...
func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

    func keyboardWillHide(notification: NSNotification) {
        self.view.frame.origin.y = 0
    }
...
}

切换键盘然后应用程序崩溃,出现以下异常:ChatDetailViewController keyboardWillShow:]:无法识别的选择器发送到实例 0x7f82fc41fdf0

错误消息乍一看似乎很清楚,但我仍然无法弄清楚我的选择器出了什么问题。没有代码警告,没有拼写错误,...

我哪里做错了?

在Swift 4中我以前用过这样的

override func viewDidLoad() {
    super.viewDidLoad()   
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
     print("keyboardWillShow")
}

@objc func keyboardWillHide(notification: NSNotification){
     print("keyboardWillHide")
}

在 Swift 5 中,他们将访问键盘通知的方式重命名为 UIResponder:

override func viewDidLoad() {
    super.viewDidLoad()   
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

您忘记添加@objc 部分:

NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillShow),
                                               name: UIResponder.keyboardWillShowNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillHide),
                                               name: UIResponder.keyboardWillHideNotification,
                                               object: nil)
@objc func keyboardWillShow(_ sender: Notification) {}
@objc func keyboardWillHide(_ sender: Notification) {}

三期:

  • 选择器创建错误(使用#selector
  • 动作签名错误(缺少下划线)
  • 动作必须标记为@objc

并且 Swift 中的类型是 Notification 没有 NS 前缀

override func viewDidLoad() {
    super.viewDidLoad()
    // do stuff...
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    // do other stuff...
}

    ...

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        self.view.frame.origin.y -= keyboardSize.height
    }
}

@objc func keyboardWillHide(_ notification: Notification) {
    self.view.frame.origin.y = 0
}

...