没有使用 Objective-C 通知 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 选择器声明的方法
No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification
在 Xcode 的最近更新后,该代码曾经有效,但不再有效。大多数 Selector(":") 都具有自动更正功能,但以下代码除外:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
其中标记了一个错误:
No method declared with Objective C selector 'keyboardWillSHow:'
这张图片显示了不同的尝试,但都失败了。
此代码的新语法是什么?
swift 语法已更改。试试这个:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);
分配 Selector
如下:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);
以及更新你想要的方法:
func keyboardWillShow(notification: NSNotification) {
//Update UI or Do Something
}
与 UIKeyboardWillHideNotification
相同的方法。
我遇到了同样的问题,并且还发现您所引用的 class 也必须是 NSObject 的子class(这不是necc. Swift 中的情况)否则你会收到消息
error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"
Swift 3 语法(就像 Sohil 上面的一样):
func someMethod(sender: Any?) {
...
}
func someBlockCallingWithSelector() {
someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged)
}
Swift 3 例:
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
// MARK: - Actions
@objc private func keyboardWillShow(notification: Notification) {
print("keyboardWillShow called")
}
@objc private func keyboardWillHide(notification: Notification) {
print("keyboardWillHide called")
}
在 Xcode 的最近更新后,该代码曾经有效,但不再有效。大多数 Selector(":") 都具有自动更正功能,但以下代码除外:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
其中标记了一个错误:
No method declared with Objective C selector 'keyboardWillSHow:'
这张图片显示了不同的尝试,但都失败了。
此代码的新语法是什么?
swift 语法已更改。试试这个:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);
分配 Selector
如下:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);
以及更新你想要的方法:
func keyboardWillShow(notification: NSNotification) {
//Update UI or Do Something
}
与 UIKeyboardWillHideNotification
相同的方法。
我遇到了同样的问题,并且还发现您所引用的 class 也必须是 NSObject 的子class(这不是necc. Swift 中的情况)否则你会收到消息
error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"
Swift 3 语法(就像 Sohil 上面的一样):
func someMethod(sender: Any?) {
...
}
func someBlockCallingWithSelector() {
someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged)
}
Swift 3 例:
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
// MARK: - Actions
@objc private func keyboardWillShow(notification: Notification) {
print("keyboardWillShow called")
}
@objc private func keyboardWillHide(notification: Notification) {
print("keyboardWillHide called")
}