2016-04-01 5 views
1

私はswiftでアプリケーションを作成しています。私は、タップジェスチャ認識機能とNSNotification機能を追加したビューコントローラをサブクラス化し、キーボードの表示を待ち受けます。私はkeyboardWillShowのセレクタをベースビューコントローラ内の関数に置きます。しかし、私のView Controllerをサブクラス化してキーボードショーを行ったとき、私のアプリはセレクタを見つけることができないと言ったNSExceptionで終了しました。なぜこれが起こったのか、どのように修正できるのか誰にも説明できますか?ここでSwift:ViewControllerをサブクラス化し、ターゲットを追加しますか?

は私のベース・ビュー・コントローラ内の機能です:

override func viewDidLoad() { 
    super.viewDidLoad() 
    setNotificationListers() 
    setTapGestureRecognizer() 
} 

deinit { 
    NSNotificationCenter.defaultCenter().removeObserver(self) 
} 

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

func setTapGestureRecognizer() { 
    let tapped = UITapGestureRecognizer(target: self, action: "closeKeyboard") 
    tapped.numberOfTapsRequired = 1 
    self.view.addGestureRecognizer(tapped) 
} 

func closeKeyboard() { 
    self.view.endEditing(true) 
} 

func keyboardWillShow() { 
    self.view.frame.origin.y += CGFloat(keyboardHeight) 
} 

func keyboardWillHide() { 
    self.view.frame.origin.y -= CGFloat(keyboardHeight) 
} 

私は私のサブクラスで何かを上書きしませんでした。どのようなものが継承されるのでしょうか?

ありがとうございました!

答えて

1

セレクタ宣言にはパラメータが必要ですが、関数にはパラメータは必要ありません。

どちらかあなたのセレクタの宣言

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

または

func keyboardWillShow(notification: NSNotification) { 
    self.view.frame.origin.y += CGFloat(keyboardHeight) 
} 

あなたの機能を変更し、keyboardWillHideのために同じことを行うから:を削除します。

0

Selectorメソッドには:があります。これは、メソッドにパラメータが必要であることを意味します。 だから、あなたはこれに2つのあなたの方法を変更する必要があります。

func keyboardWillShow(notification: NSNotification) { 
    self.view.frame.origin.y += CGFloat(keyboardHeight) 
} 

func keyboardWillHide(notification: NSNotification) { 
    self.view.frame.origin.y -= CGFloat(keyboardHeight) 
} 

とにかく、Xcodeの7.3はselectorを実装する方法を変更あり。そして、キーボードを押し上げるのにこの偉大なライブラリhttps://github.com/hackiftekhar/IQKeyboardManagerを使うことができます。非常に使いやすい。

関連する問題