user2308343
user2308343

Reputation: 107

Taking Text From a UIAlertView

There seems to be something missing, but the below code is generating nil values for both title and title1 (even though it launches the right alert type correctly and doesn't indicate any warning or error). What could be the problem with this implementation of UIAlertView?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
UITextField *title1 = [alert textFieldAtIndex:0];
[alert show];
title1= [alert textFieldAtIndex:0];
NSString *title = title1.text;
NSLog(@"The name is %@",title);
NSLog(@"Using the Textfield: %@",[[alert textFieldAtIndex:0] text]);

Upvotes: 3

Views: 5491

Answers (4)

RStack
RStack

Reputation: 230

In iOS 8 UIAlertview has been deprecated. UIAlertController is used instead of that.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Add Fruit" message:@"Type to add a fruit" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
    textField.placeholder = @"Fruit";
}];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                               style:UIAlertActionStyleDefault
                                             handler:^(UIAlertAction *action) {
                                                 UITextField *textFiel = [alert.textFields firstObject];
                                                 [fruits addObject:textFiel.text];
                                             }];
[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];

Upvotes: 2

RJR
RJR

Reputation: 1072

Present alert somewhere in you code and set the view controller from it was presented as the delegate for your UIAlertView.Then implement the delegate to receive the event.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score" message:@"Score Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.alertViewStyle= UIAlertViewStylePlainTextInput;
[alert show];

Implement the delegate method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
   UITextField *textField = [alertView textFieldAtIndex:0];
   NSString *title = textField.text;
   NSLog(@"The name is %@",title);
   NSLog(@"Using the Textfield: %@",[[alertView textFieldAtIndex:0] text]);
}

Upvotes: 11

Undo
Undo

Reputation: 25697

You'll need to wait for the delegate callback alertView:clickedButtonAtIndex to get the text - keep a reference to your text field and show the alert, setting its delegate to self. When that method is called, the user has pressed the button to say they are finished entering text, so it's probably now safe to grab the text out of the text field.

Upvotes: 0

omz
omz

Reputation: 53551

[alert show] returns immediately, before the user could have entered any text in the text field. You need to get the text after the alert has been dismissed by setting its delegate and implementing alertView:clickedButtonAtIndex: (for example, there are a couple of other possibilities).

Upvotes: 6

Related Questions