Assam AlZookery
Assam AlZookery

Reputation: 479

IOS 9 - How do i get a value of textField in an UIAlertController?

I'm trying to get a textField value from UIAlertController, but whenever I try to enter the value (Name) and display it on label, the output doesn't show anything.

- (IBAction)button:(id)sender {
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Test" message:@"Please Enter your name " preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Person Name ";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = NO;

    }];
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"ENTER, please"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    //Handle the ENTER button 
                                   // how to Display the name in the label

                                }];
    UIAlertAction* noButton = [UIAlertAction
                               actionWithTitle:@"No, thanks"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                               {
                                   //Handle no button. 
                               }];


    [alert addAction:yesButton];
    [alert addAction:noButton];

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

}

Upvotes: 0

Views: 613

Answers (1)

Richmond Watkins
Richmond Watkins

Reputation: 1370

Quick swift example I pulled from an old project

     alert.addAction(UIAlertAction(title: "Send", style:   UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
        if let textFields = alert.textFields {
            if let textField = textFields.first {

            }
        }
    }))

In your application it would like something like this:

UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"ENTER, please"   style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
    NSArray *textFields = alert.textFields;
    // Probably smart to do some nil checking here
    NSLogv(@"%@", textFields.first.text);
    // add to label like so
    label.text = textFields.first.text;
}];

Upvotes: 1

Related Questions