2015-10-05 170 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) 
    { 

    } 
} 
相關問題