Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController get text

I am trying to get the text from the UIAlertController textfield. Can someone tell me what I did wrong as its not working. I get a NIL return.

- (IBAction)btnMakeRec {

        UIAlertController *alert= [UIAlertController
                                   alertControllerWithTitle:@"Recipe Name?"
                                   message:@""
                                   preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action){


                                               UITextField *temp = alert.textFields.firstObject;

                                               RecipeDesc.text = temp.text;
                                               // HERE temp is Nil


                                               RDescription = [[NSString alloc ] initWithFormat:@"%@", RecipeDesc.text];


                                                   }];

        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {

                                                         //  NSLog(@"cancel btn");

                                                           [alert dismissViewControllerAnimated:YES completion:nil];

                                                       }];

        [alert addAction:ok];
        [alert addAction:cancel];

        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;
        }];

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

    }

}
like image 488
Mark Worsnop Avatar asked Dec 29 '14 17:12

Mark Worsnop


1 Answers

Here's the 'clean copy/paste' version:

Swift 3+:

let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: .alert)

let ok = UIAlertAction(title: "OK",
                       style: .default) { (action: UIAlertAction) in
                        
                        if let text = alert.textFields?.first?.text {
                            
                            print("And the text is... \(text)")
                            
                        }
                        
                        
}

let cancel = UIAlertAction(title: "Cancel",
                           style: .cancel,
                           handler: nil)

alert.addTextField { (textField: UITextField) in
    
    textField.placeholder = "Text here"
    
}

alert.addAction(ok)
alert.addAction(cancel)

self.present(alert, animated: true, completion: nil)

Swift 2:

let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: UIAlertControllerStyle.Alert)

let ok = UIAlertAction(title: "OK",
                       style: UIAlertActionStyle.Default) { (action: UIAlertAction) in
                        
                        if let alertTextField = alert.textFields?.first where alertTextField.text != nil {
                            
                            print("And the text is... \(alertTextField.text!)!")
                            
                        }
                        
                        
}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.Cancel,
                           handler: nil)

alert.addTextFieldWithConfigurationHandler { (textField: UITextField) in
    
    textField.placeholder = "Text here"
    
}

alert.addAction(ok)
alert.addAction(cancel)

self.presentViewController(alert, animated: true, completion: nil)

Objective C:

UIAlertController *alert = [UIAlertController
                           alertControllerWithTitle: @"Alert Title"
                           message: @"Alert message"
                           preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault
                                           handler:^(UIAlertAction *action){
                                               
                                               
                                               UITextField *alertTextField = alert.textFields.firstObject;
                                               
                                               NSLog(@"And the text is... %@!", alertTextField.text);
                                               
                                           }];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                               handler: nil];


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.placeholder = @"Text here";

}];

[alert addAction:ok];
[alert addAction:cancel];

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

Previous answer:

Edit: - Please note: Currently, original question seems to be working as is.

To clarify:

The UIAlertController instance should hold the textField in its textFields array after calling the addTextFieldWithConfigurationHandler.

UIAlertController *alertController = ...// Create alert

// Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController'

UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) {
    
    // alertController.textFields should hold the alert's text fields.
}

If for some reason it is not, please shed more light (since this issue is still getting attention). It seems (by some of the comments) that some people had some issues with this, but did not provide information beyond 'it doesn't work'.


Original answer:

Your code looks fine, it should work.

Another way is to define a UITextField before UIAlertController *alert=...

UITextField *myTf;

Pass the textField from addTextFieldWithConfigurationHandler:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;

            myTf = textField;
        }];

Then, in:

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action){...
//UITextField *temp = alert.textFields.firstObject;

// Get the text from your textField
NSString *temp = myTf.text;

-- EDIT

Original poster's code now works as is (tested under Xcode 7, iOS 9). Could have been a bug in previous version. Could be that in previous version the textField was held by a weak reference and got released unless another strong pointer was holding it.

like image 79
bauerMusic Avatar answered Oct 21 '22 00:10

bauerMusic