2015-10-05 111 views
1

我正在尝试在我的KeyboardScrollController类中获取键盘通知,但我无法识别选择器UIKeyboardWillHideNotificationUIKeyboardDidShowNotificationUIKeyboardWillHideNotification上无法识别的选择器

这是我简单的实现:

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中使用它。

回答

1

啊,它突然打到我,这个问题可能是什么。我不得不从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) 
    { 

    } 
} 
相关问题