UITextField cannot get keyboard when mulple alertView popup?

319 Views Asked by At

In order to satisfy the need of my leader, I have to pop up multiple alertViews (of style UIAlertViewStyleSecureTextInput when a signal come).
But I am encountering a strange error... After multiple alertView popups, I lose the focus of another textField.

the following code:

if (!didDisplayAlarm && (timeInterval < -_alertStopTime) && (self.isMainViewController)) {
    didDisplayAlarm = YES; //first method

    if (_alertView) {
        [_alertView dismissWithClickedButtonIndex:0 animated:NO]; //second method
    }

    _alertView = [[UIAlertView alloc] initWithTitle:title
                                            message:message
                                           delegate:self
                                  cancelButtonTitle:@"ignore"
                                  otherButtonTitles:@"check", nil];
    _alertView.tag = kAlarmTag;

    alarmUser = [NSString stringWithFormat:@"%d", [[dict objectForKey:@"alarmUser"] intValue]];
    alarmPassword = nil;

    _alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
    
    UITextField *alertTextField = [_alertView textFieldAtIndex:0];
    alertTextField.keyboardAppearance = UIKeyboardAppearanceDefault;
    alertTextField.keyboardType = UIKeyboardTypeNumberPad;
    [alertTextField setPlaceholder:@"please input password"];
    [_alertView show];
}

I tried three method to avoid such issue:

  1. set a bool var didDisplayAlarm to reduce the popup of alertView
  2. use dismiss method of alertView
  3. use textfield resignFirstResponder in the alertView delegate

My environment is IOS 6.1.3 but all doesn't work.

2

There are 2 best solutions below

0
On

When you click ok/cancel in alertview you need to resign your keyboard.

to get UITextField from alertview in clickedButtonAtIndex: delegate method we have two options

Option 1 - loop the subviews of alert and get the instance of textfiled and resign it.

for (UIView* view in alertView.subviews)
{
    if ([view isKindOfClass:[UITextField class]])
    {
        UITextField* textField = (UITextField*)view;
        NSLog(@"text:[%@]", textField.text);
        break;
    }
}

Option 2 - you can get the textfiled instance by [alertView textFieldAtIndex:0].text. I think this can be done only if the alert style is UIAlertViewStylePlainTextInput.

3
On

The problem here is you are using resignFirstResponder but your textfield is not identifying which one to resign. So when you are resigning the textfield then you have to make other textfield to be first responder. For example,lets say if you have multiple alertView and each contains separate textfield. So for determining the textfield just set the tag Value on textfield and on the basis of tag value resign the textfield and make other one textfield to be the first responder.