UIKeyboardWillHideNotification上无法识别的选择器
问题描述:
我正在尝试在我的KeyboardScrollController
类中获取键盘通知,但我无法识别选择器UIKeyboardWillHideNotification
和UIKeyboardDidShowNotification
。UIKeyboardWillHideNotification上无法识别的选择器
这是我简单的实现:
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)
{
}
}