2016-08-31 22 views
1

我在swift中有以下訂閱。我需要知道鍵盤何時會顯示以移動視圖。它編譯和按預期工作,但我不知道如何擺脫這個警告。 「目標C選擇器鍵盤顯示沒有方法」如何在訂閱NSNotifications時使用Objective C選擇器作爲參數?

// Subscribing class to recieve the notification 
func subscribeToKeyboardNotifications() { 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil) 
} 
// Shifting view's frame up 
func keyboardWillShow(notification: NSNotification) { 
    view.frame.origin.y -= getKeyboardHeight(notification) 
} 
// Getting keyboard height 
func getKeyboardHeight(notification: NSNotification) -> CGFloat { 
    let userInfo = notification.userInfo 
    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect 
    return keyboardSize.CGRectValue().height 
} 

在此先感謝您的任何建議!

+0

請檢查這個答案,http://stackoverflow.com/questions/36366116/no-method-declared-with-objective-c-selector-for-notification-uikeyboardwillshow – Sandy

回答

1

使用#selector

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 
+0

感謝您的回答,並指出此問題已得到解答:)我沒有在Google上找到它,或者在提出問題時找不到它。 –

相關問題