は、キーボードの通知に耳を傾け、あなたのtableViewの外のすべてのイベントを受け取りますタップ認識の作成:次に
- (void)viewDidLoad
{
[super viewDidLoad];
...
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:
UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:
UIKeyboardWillHideNotification object:nil];
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
...
}
をキーボードの通知方法で、ジェスチャ認識機能をビューから追加して削除します。あなたのジェスチャー認識のアクションメソッドで
//add gesture recognizer when keyboard appears
-(void)keyboardWillShow:(NSNotification *) note {
[self.view addGestureRecognizer:tapRecognizer];
}
//remove it when keyboard disappears
-(void)keyboardWillHide:(NSNotification *) note
{
[self.view removeGestureRecognizer:tapRecognizer];
}
あなたはキーボードを閉じ、すべての最初の応答者を辞任:
-(IBAction)dismissKeyboard:(id)sender
{
//this removes ALL firstResponder from view
[self.view endEditing:TRUE];
}
は、いくつかの点で、キーボード通知を聞いて終了することを忘れないでください:
[[NSNotificationCenter defaultCenter] removeObserver:self];