UIAlertController get text

I am trying to get text from a UIAlertController text box. Can someone tell me what I did wrong because it is not working. I get a NIL refund.

- (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]; } } 
+5
source share
1 answer

Here is the version of "clean copy / paste":

Swift 3:

 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, alertTextField.text != nil { print("And the text is... \(alertTextField.text!)!") } } let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.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) 

Goal 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: at the moment, the original question is working as is.

To clarify:

The UIAlertController instance must contain the textField in the textFields array after calling addTextFieldWithConfigurationHandler .

 UIAlertController *alertController = ...// Create alert // Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController' UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) { // alertController.textFields should hold the alert text fields. } 

If for some reason this is not the case, please shed more light (since this issue is still attracting attention). It seems (according to some comments) that some people have some problems with this, but do not provide information other than "it does not work."


Original answer:

Your code looks good, it should work.

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

UITextField *myTf;

Pass 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

The original poster code now works as (tested in Xcode 7, iOS 9). Perhaps this was a bug in the previous version. Maybe in the previous version textField was held by a weak standard and was released if another strong pointer did not hold it.

+28
source

Source: https://habr.com/ru/post/1210062/


All Articles