没有使用Objective-C选择器声明的方法用于通知UIKeyboardWillShowNotification和uikeyboardwillhi。

时间:2023-01-15 18:38:08

After the recent update of Xcode, this code that used to work no longer works. Most of the Selector(":") has an auto correction with the exception for this code:

在最近更新了Xcode之后,以前可以工作的代码不再工作。大多数选择器(":")都有一个自动校正,除了这段代码:

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

which flags an error:

标志一个错误:

No method declared with Objective C selector 'keyboardWillSHow:'

没有使用Objective - C选择器'keyboardWillSHow:'声明的方法

This image show different attempts which have all failed.

这张照片显示了不同的尝试都失败了。

没有使用Objective-C选择器声明的方法用于通知UIKeyboardWillShowNotification和uikeyboardwillhi。

What is the new syntax for this code?

这段代码的新语法是什么?

5 个解决方案

#1


11  

Assign the Selector as below:

分配选择器如下:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);

And the method to update what you want:

以及更新你想要的东西的方法:

func keyboardWillShow(notification: NSNotification) {

     //Update UI or Do Something

}

Same way you can do for UIKeyboardWillHideNotification.

同样的方法,你可以为uikeyboardwillhi认证。

#2


1  

Swift 3 example:

斯威夫特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")
}

#3


0  

The swift syntax changed. Try this:

斯威夫特的语法变化。试试这个:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);

#4


0  

I have had same issues and also find out that the class you refer on must also be subclassed from NSObject (which is not necc. the case in Swift) Otherwise you get the message

我也遇到过同样的问题,并且发现您所引用的类也必须从NSObject(它不是necc)中进行子类化。案件在斯威夫特)否则你得到信息

error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"

#5


0  

Swift 3 syntax (just like Sohil's above):

Swift 3语法(就像上面的Sohil):

    func someMethod(sender: Any?) {
      ...
    }

    func someBlockCallingWithSelector() {
      someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged) 
    }

#1


11  

Assign the Selector as below:

分配选择器如下:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);

And the method to update what you want:

以及更新你想要的东西的方法:

func keyboardWillShow(notification: NSNotification) {

     //Update UI or Do Something

}

Same way you can do for UIKeyboardWillHideNotification.

同样的方法,你可以为uikeyboardwillhi认证。

#2


1  

Swift 3 example:

斯威夫特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")
}

#3


0  

The swift syntax changed. Try this:

斯威夫特的语法变化。试试这个:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #Selector(ClassThatHasTheSelector.keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil);

#4


0  

I have had same issues and also find out that the class you refer on must also be subclassed from NSObject (which is not necc. the case in Swift) Otherwise you get the message

我也遇到过同样的问题,并且发现您所引用的类也必须从NSObject(它不是necc)中进行子类化。案件在斯威夫特)否则你得到信息

error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C"

#5


0  

Swift 3 syntax (just like Sohil's above):

Swift 3语法(就像上面的Sohil):

    func someMethod(sender: Any?) {
      ...
    }

    func someBlockCallingWithSelector() {
      someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged) 
    }