No Keyboard with UIAlertViewStylePlainTextInput ?

2k views Asked by At

I have a problem with the UIAlertViewStylePlainTextInput. I get my Alert with the textfield but there is no Keyboard coming up..? Here is my code:

UIAlertView *enter = [[UIAlertView alloc] initWithTitle:@"Highscores" message:@"Please Enter Your Name" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Abbrechen" nil];

enter.alertViewStyle = UIAlertViewStylePlainTextInput;


UITextField *theTextField =  [enter textFieldAtIndex:0];
theTextField.placeholder = @"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;


[enter show];

I have tried without the

UITextField *theTextField =  [enter textFieldAtIndex:0];
theTextField.placeholder = @"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;

part but still there is no keyboard coming up.

(I've tested it on my device and on the simulator)

Do you have any ideas about that?

thank you in advance.

3

There are 3 answers

1
UndergroundFox On

Might fix your problem as it solved mine:

[enter resignFirstResponder];
0
michael23 On

I had the problem because I was triggering the alert when a UITextField delegate method textFieldDidBeginEditing was called in my view and then I would fill this with the content of the Alert view text field. That meant that the UITextField in my view became first responder and I couldn't find a way to resign. So if you have the problem it is because something else is becoming first responder before the UIAlert is displayed. Think about how you trigger the alert.

I resigned to using a tap gesture on my UITextField which then triggers the UIAlertView with UIAlertViewStylePlainTextInput and it works fine.

Some code below:

-(void)addTextFieldToView{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textAlignment = UITextAlignmentCenter;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAlertWithTextField:)];
    [textField addGestureRecognizer:tapGestureRecognizer];
    [self.view addSubview:textField];
}

-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil]; 
    [dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [dialog show]; 
}
0
Madhu S. Kapoor On

Try below code:

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Your name?"
                                                  message:nil
                                                 delegate:nil 
                                        cancelButtonTitle:@"Cancel" 
                                        otherButtonTitles:@"OK", nil];

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];

Works on iOS5