El Tomato
El Tomato

Reputation: 6707

UIAlertController Does Not Return User Input

I'm using UIAlertController for the first time. I need to prompt the user for an input. Anyway, the following is what I have.

- (void)showAlert {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Hello!" message:@"Type something.") preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *noButton = [UIAlertAction actionWithTitle:@"Cancel") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }];
    UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"Enter") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //NSString *input = alert.textFields[0].text;
        //NSLog(@"input was '%@'", input);
        UITextField *textField = alert.textFields[0];
        NSLog(@"%@",textField.text); // <====== It's not returned

    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        [textField addTarget:self action:@selector(alertTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [yesButton setEnabled:NO];
    }];
    [alert addAction:noButton];
    [alert addAction:yesButton];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertTextFieldDidChange:(UITextField *)sender {
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        UITextField *textfield = alertController.textFields.firstObject;
        UIAlertAction *okAction = alertController.actions.lastObject;
        okAction.enabled = [self validateName2:textfield.text:5]; // function for validating user input
    }
}

For some reason, when the user tap Yes, the application doesn't return user input. In fact, Xcode dashes 'text' as in 'NSString *input = alert.textFields[0].text. A strange thing is that UIAlertView doesn't return user input. That's why I've switched to UIAlertController.

enter image description here

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == 101) {
        if (buttonIndex == 1) {
            NSString *username = [[alertView textFieldAtIndex:0] text];
            NSLog(@"%@",username); // <====== It's never returned.
        }
    }
}

Anyway, I wonder what I'm doing wrong with UIAlertController?

Muchos thankos

p.s. I'm using Xcode 6.4 (6E35b). This issue may be caused by Xcode. If I debug the code with the iPad2 simulator, it'll actually return user input while it doesn't if I use the iPhone4S simulator.

Upvotes: 0

Views: 683

Answers (2)

rushisangani
rushisangani

Reputation: 3395

Use this for textField in alertcontroller

__block typeof(self) weakSelf = self;
UIAlertController *alertController;

alertController = [UIAlertController alertControllerWithTitle:title
                                     message:message
                                     preferredStyle:UIAlertControllerStyleAlert];


[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.tag = 1001;
     textField.delegate = weakSelf;
     textField.placeholder = @"";

     [textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:)
         forControlEvents:UIControlEventEditingChanged];
 }];


//OK Button
UIAlertAction *okAction = [UIAlertAction
                           actionWithTitle:@"OK"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction *action)
                           {
                               UITextField *textField = alertController.textFields.firstObject;

                               // Do stuff here
                           }];



[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

- (void)alertTextFieldDidChange:(UITextField *)sender
 {
   alertController = (UIAlertController *)self.presentedViewController;
   if (alertController)
   {
     UITextField *textField = alertController.textFields.firstObject;
     UIAlertAction *okAction = alertController.actions.lastObject;

     okAction.enabled = textField.text.length > 0; // condition to enable button
   }
}

 #pragma mark - UITextField delegate methods

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
 }

Upvotes: 2

A.J. S.
A.J. S.

Reputation: 218

I would recommend using UITextFieldDelegate to handle the textField!

Set textField.delegate = self and then use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method to do what you do in alertTextFieldDidChange if the replacementString has a length of > 0.

You are provided with delegate methods for UITextField so you don't have to create the selectors yourself, I recommend you use them to their fullest!

Here are the docs in case you want to know more! https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/

Upvotes: 1

Related Questions