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

标签 swift keyboard uitextfield xcode7 swift2

在最近更新 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:'

这张图片显示了不同的尝试,但都失败了。

enter image description here

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

最佳答案

如下所示分配选择器:

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

以及更新您想要的内容的方法:

func keyboardWillShow(notification: NSNotification) {

     //Update UI or Do Something

}

UIKeyboardWillHideNotification 相同的方法。

关于swift - 没有使用 Objective-C 选择器声明用于通知 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366116/

相关文章:

swift - 简单的 UITableView 源文件中的编辑器占位符错误

ios - 如何制作一个包含类型的数组?

android - EditText 不会自动启动

python 3 : how to use the press Ctrl+X (cut) and Ctrl+V using pynput?

iphone - iPhone:为什么发送消息resignFirstResponder会移除键盘?

ios - 有没有办法从表格 View 单元格的文本字段中获取值以在另一个表格 View 单元格中使用它?

arrays - 从数组中删除对象

objective-c - iPad : How to know Return key of iPad keyboard is pressed ? 请检查图像

ios - SetDecimalSeparator 在真实设备中不起作用

swift - UITextfield 键盘外观的扩展