Keyboard not dismissing when UIAlertView with textfields is dismissed


I would make sure to show the alert late in the view life cycle of the view controller that's presenting it, eg

- (void)viewDidAppear {
    [super viewDidAppear];
    [self performSelector:@selector(showTheAlert) afterDelay:0.0];
}

- (void)showTheAlert {
    // alloc, init and show the alert here
}

And I'd use a delegate callback to give the textView first responder, eg

- (void)didPresentAlertView:(UIAlertView *)alertView {
    [theUsernameTextField becomeFirstResponder];
}

And I'd use the delegate for any dismissal to resign. There's no downside to resigning first responder on a text field that isn't the first responder, so just resign both.

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {

    [theUsernameTextField resignFirstResponder];
    [thePasswordTextField resignFirstResponder];
}

You can conform self as a delegate of your UIAlertView once you create one. After that in:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

method you can get reference to your text field and you can call resignFirstResponder on it.


实现下面的方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
      UITextField *txtField = [alertView textFieldAtIndex:buttonIndex];
      [txtField resignFirstResponder];
}
链接地址: http://www.djcxy.com/p/44228.html

上一篇: 如何消除键盘而不失焦点。 或者至少显示光标

下一篇: 当带有文本字段的UIAlertView被解除时,键盘不会消失