ios - 如何将 NSNotification 作为参数传递给 Swift 中的选择器函数

标签 ios swift nsnotifications

我正在尝试实现 UIKeyboardWillShowNotification 以在键盘出现时处理我的 View 位置。 我添加我的观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil)

然后我有 keyboardWillShow 功能:

func keyboardWillShow(notification: NSNotification){
    //Need to access to the notification here
}

在函数 keyboardWillShow 中,我需要接收 NSNotification 才能访问用户信息,但出现此错误:

"unrecognized selector sent to instance 0x7fd993e7d020"

最佳答案

您忘记了冒号 :。将其更改为:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

关于ios - 如何将 NSNotification 作为参数传递给 Swift 中的选择器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326601/

相关文章:

ios - Parse.com 好友关系

ios - SwiftUI View 中的间距不正确

swift - 我可以从该协议(protocol)中获取符合该协议(protocol)的类型的名称吗?

ios - 相同的代码在一个项目中有效,在另一个项目中无效

iphone - 在 iOS 7 中未观察到 UIAccessibilityInvertColorsStatusDidChangeNotification

iOS 7 - 仅针对 Xamarin 中内置的应用程序将 View 显示为纵向

iphone - TableView addGesture 到 UIImageView 以了解哪个单元格被点击

ios - 如何在 iOS 11 中将导航栏设置为透明

ios - 如何将 "move view with keyboard"函数实现到文本字段中

objective-c - 分类为 NSNotification 观察者?