2016-04-01 17 views
5

Xcodeの最近の更新後、このコードは機能しなくなりました。セレクター(「:」)のほとんどは、このコードの例外を除いて自動修正を持っていますNo Objective-Cで通知されるメソッド通知UIKeyboardWillShowNotificationおよびUIKeyboardWillHideNotificationのセレクタ

override func viewDidLoad() { 
    super.viewDidLoad() 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); 
} 

たフラグエラー:

No method declared with Objective C selector 'keyboardWillSHow:'

この画像を表示すべて失敗しているさまざまな試み。

enter image description here

このコードのための新しい構文は何ですか?

答えて

10

は、以下のようにSelectorを割り当てます。

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

そして、あなたが望むものを更新する方法:あなたがUIKeyboardWillHideNotificationのために何ができる

func keyboardWillShow(notification: NSNotification) { 

    //Update UI or Do Something 

} 

同じように。

+1

Sohilに感謝します。できます –

0

迅速な構文が変更されました。これを試してみてください:

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

それでもエラーにフラグを立てます:タイプ: 'SignInViewController'に 'KeyboardWillShow'というメンバーはありません。 Xcodeを更新していない限り、このコードは動作します。 –

+0

そのメソッドが見つかりません。そのクラスでkeyboardWillShowが使用可能であり、名前が一致していることを確認してください(エラーメッセージの大文字キーボードに気付きました)。メソッド名を大文字で始めるのは非正統です。 –

+0

keyBoardWillShowは大文字ではありませんでした。誤字脱字しました。上記のエラーと同じエラーのフラグ。 –

0

私はそうでなければ、あなたがメッセージ

error: argument of '#selector' refers to instance method 'yourMethod(notification:)' that is not exposed to Objective-C" 
0

を取得し、同じ問題を持っていたし、また、あなたが上で参照するクラスも(NECCではありません。スウィフトにおけるケース)NSObjectのからサブクラス化されなければならないことを見つけるしています(ちょうどSohilの上記のような)スウィフト3構文:

func someMethod(sender: Any?) { 
     ... 
    } 

    func someBlockCallingWithSelector() { 
     someObject.addTarget(self, action: #selector(someMethod), for: .valueChanged) 
    } 
1

スウィフト3例:

NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil); 
NotificationCenter.default.addObserver(self, selector: #selector(YourClass.keyboardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil); 

// MARK: - Actions 

@objc private func keyboardWillShow(notification: Notification) { 
    print("keyboardWillShow called") 
} 

@objc private func keyboardWillHide(notification: Notification) { 
    print("keyboardWillHide called") 
} 
関連する問題