UIKeyboardWillHideNotification 上无法识别的选择器
Unrecognized selector on UIKeyboardWillHideNotification
我正在尝试在我的 KeyboardScrollController
class 中获取键盘通知,但我得到 UIKeyboardWillHideNotification
和 UIKeyboardDidShowNotification
的无法识别的选择器。
这是我的简单实现:
public class KeyboardScrollController
{
public func subscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
public func unsubscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil);
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil);
}
//MARK: Keyboard events
public func keyboardWasShown(notification: NSNotification)
{
}
public func keyboardWillHide(notification: NSNotification)
{
}
}
但是每次出现键盘时它都会崩溃并出现以下错误:
*** NSForwarding: warning: object 0x7fdc8d882730 of class 'KeyboardScrollController' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[KeyboardScrollController keyboardWillHide:]
我试过 Selecor("keyboardWillHide:")
,但没有任何区别。
这里有什么问题?我在 Objective-C 中实施了几次,但我无法在 Swift 中使用它。
啊,突然想到,这到底是什么问题。我必须继承 NSObject
才能使其工作:
public class KeyboardScrollController : NSObject
{
public func subscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
public func unsubscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil);
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil);
}
//MARK: Keyboard events
public func keyboardWasShown(notification: NSNotification)
{
}
public func keyboardWillHide(notification: NSNotification)
{
}
}
我正在尝试在我的 KeyboardScrollController
class 中获取键盘通知,但我得到 UIKeyboardWillHideNotification
和 UIKeyboardDidShowNotification
的无法识别的选择器。
这是我的简单实现:
public class KeyboardScrollController
{
public func subscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
public func unsubscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil);
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil);
}
//MARK: Keyboard events
public func keyboardWasShown(notification: NSNotification)
{
}
public func keyboardWillHide(notification: NSNotification)
{
}
}
但是每次出现键盘时它都会崩溃并出现以下错误:
*** NSForwarding: warning: object 0x7fdc8d882730 of class 'KeyboardScrollController' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[KeyboardScrollController keyboardWillHide:]
我试过 Selecor("keyboardWillHide:")
,但没有任何区别。
这里有什么问题?我在 Objective-C 中实施了几次,但我无法在 Swift 中使用它。
啊,突然想到,这到底是什么问题。我必须继承 NSObject
才能使其工作:
public class KeyboardScrollController : NSObject
{
public func subscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
public func unsubscribeForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil);
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil);
}
//MARK: Keyboard events
public func keyboardWasShown(notification: NSNotification)
{
}
public func keyboardWillHide(notification: NSNotification)
{
}
}