没有用Objective-C Selector声明的方法用于通知UIKeyboardWillShowNotification和UIKeyboardWillHideNotification

Mar*_*n Q 5 keyboard uitextfield swift xcode7 swift2.2

在最近更新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);
}
Run Code Online (Sandbox Code Playgroud)

标记错误:

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

此图像显示了所有失败的尝试.

在此输入图像描述

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

Soh*_*mon 11

分配Selector如下:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil);
Run Code Online (Sandbox Code Playgroud)

以及更新所需内容的方法:

func keyboardWillShow(notification: NSNotification) {

     //Update UI or Do Something

}
Run Code Online (Sandbox Code Playgroud)

你也可以这样做UIKeyboardWillHideNotification.