3

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.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
wintersan
  • 279
  • 1
  • 4
  • 10

3 Answers3

4

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]; 
}
michael23
  • 3,746
  • 1
  • 15
  • 10
1

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

Madhu S. Kapoor
  • 345
  • 1
  • 5
  • 11
0

Might fix your problem as it solved mine:

[enter resignFirstResponder];
Anne
  • 26,765
  • 9
  • 65
  • 71
UndergroundFox
  • 1,011
  • 2
  • 12
  • 11
  • 1
    Do you mean `[enter becomeFirstResponder]` ? Your current code hides the keyboard – BBog Sep 20 '12 at 07:15