6

と のオブザーバーがUIKeyboardWillShowNotificationありUIKeyboardWillHideNotificationます。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

viewController が現在表示されていない場合に機能することを除いて、すべて機能します。

と比較してみましたが、モーダルビューコントローラーの下にあるモーダルビューがself.navigationcontroller.topViewController表示されている場合、これは機能しません。topViewController

4

2 に答える 2

7

使用している場合は、ビューが内部で表示されるUIViewControllerようになったときにキーボード通知用にインスタンスをviewWillAppear:登録し、ビューが内部で非表示になったときに登録を解除viewWillDisappear: できます。この方法では、ビューが表示されていないときに通知を受け取ることはありません。

お役に立てれば!

于 2013-06-06T16:10:03.047 に答える
3

viewController が表示されているときにのみその通知に反応したい場合は、関数の最初に表示されているかどうかを確認してください。

- (void)keyboardWillShow:(NSNotification *)notification
{
    if ([self.view window]) //means is visible
        //do something
    else 
        //return
}
于 2013-06-06T15:52:59.130 に答える