没有使用Objective-C选择器声明通知UIKeyboardWillShowNotification和UIKeyboardWillHideNotification的方法 [英] No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification

查看:528
本文介绍了没有使用Objective-C选择器声明通知UIKeyboardWillShowNotification和UIKeyboardWillHideNotification的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近更新Xcode之后,该曾经起作用的代码不再起作用.除以下代码外,大多数Selector(:")都具有自动更正功能:

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:

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

标记错误:

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

No method declared with Objective C selector 'keyboardWillSHow:'

此图显示了不同的尝试,但都失败了.

This image show different attempts which have all failed.

此代码的新语法是什么?

What is the new syntax for this code?

推荐答案

按以下方式分配Selector:

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

}

您可以为UIKeyboardWillHideNotification做同样的事情.

Same way you can do for UIKeyboardWillHideNotification.

这篇关于没有使用Objective-C选择器声明通知UIKeyboardWillShowNotification和UIKeyboardWillHideNotification的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆