2012-05-08 7 views
3

ユーザ/パスワードログインコントロールにUITableViewを使用します。今は、ログインボタンとともに、ビュー上の唯一のコントロールです。内部をクリックすると、テキスト編集コンテンツがアクティブになり、キーボードがポップアップして入力できます。ただし、編集を中止する方法はありません。 UIViewの白い部分を外側にクリックして、私のUITableVIew内のテキストエディタからフォーカスを外し、キーボードが再び見えなくなるようにしたいと思います。キーボードの外にUITableViewをフォーカスする

どうすればよいですか?

答えて

3

は、キーボードの通知に耳を傾け、あなたの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]; 
1

あなたはアルファチャンネルがEQUILゼロでボタンを追加することができますと呼ばれる機能

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyBoard)]; 
[tap setNumberOfTapsRequired:1]; 
[self.view addGestureRecognizer:tap]; 
5

でシングルタップGestureRecognizerを追加して、キーボードを辞任します。あなたのログインとパスワードフィールドの後ろにあります。以下の方法でこのボタンに設定アクション:あなたのビューコントローラを作るviewDidLoad

-(void)hideKeyboard:(id)sender 
{ 
    [self.username resignFirstResponder]; 
    [self.password resignFirstResponder]; 
} 
関連する問題