Ios 类型';ChatViewController';没有成员';showOrHideKeyboard&x27;

Ios 类型';ChatViewController';没有成员';showOrHideKeyboard&x27;,ios,swift,Ios,Swift,我似乎找不到我所做的功能 错误:类型“ChatViewController”没有成员“showOrHideKeyboard” 代码: 您应该用objc 换行 func showOrHideKeyboard(notification: NSNotification) { } 这条线 @objc func showOrHideKeyboard(_ notification: NSNotification) { } 使用以下任一选项: @objc func showOrHideKeyboard(_

我似乎找不到我所做的功能

错误:类型“ChatViewController”没有成员“showOrHideKeyboard”

代码:


您应该用
objc

换行

func showOrHideKeyboard(notification: NSNotification) {
}
这条线

@objc func showOrHideKeyboard(_ notification: NSNotification) {
}
使用以下任一选项:

@objc func showOrHideKeyboard(_ notification: NSNotification) 
#selector(ChatViewController.showOrHideKeyboard(_:))
或:

请注意函数签名中的
#selector

swift最新版本似乎不再支持带有函数名但没有参数名的#selector”(35;:)”

只需替换并填充参数名称或使用自动完成键入声明:

#selector(ViewController.showOrHideKeyboard(notification:)

优化您的代码,如下所示

在ViewDidLoad()中

创建一个函数

// MARK: - Notification Observered
    /// Notification received
    @objc func notificationObservered(notification: NSNotification) {

        switch notification.name {
        case NSNotification.Name.UIKeyboardWillShow:
            break
        case NSNotification.Name.UIKeyboardWillHide:
            // let info = notification.userInfo!
            // let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
           break
        default:
            break
        }
    }

@JohnDoe您是否为showOrHideKeyboard方法添加了objc?@JohnDoe答案已更新,请检查并接受答案。嗯,答案仍然相同?那就不必了work@JohnDoe请检查方法声明。要查看您的信息,请选中编辑历史请记住下划线
\uuu
,如果不将其放入,则它将不起作用
#selector(ViewController.showOrHideKeyboard(notification:)
[NSNotification.Name.UIKeyboardWillShow,
         NSNotification.Name.UIKeyboardWillHide].forEach { (notificationName) in
            NotificationCenter.default.addObserver(self, selector: #selector(notificationObservered(notification:)),
                                                   name: notificationName, object: nil)
        }
// MARK: - Notification Observered
    /// Notification received
    @objc func notificationObservered(notification: NSNotification) {

        switch notification.name {
        case NSNotification.Name.UIKeyboardWillShow:
            break
        case NSNotification.Name.UIKeyboardWillHide:
            // let info = notification.userInfo!
            // let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
           break
        default:
            break
        }
    }