I'm changing everything over from AlertView to AlertController, but I can't find anything online for objective c that retrieves what the user inputs in a text field for the AlertController. Here is what I have:
if ([UIAlertController class]) {
UIAlertController *alertControllerK2 = [UIAlertController
alertControllerWithTitle:@"\u00A0"
message:@"Please enter the first number."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *K2okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil];
[alertControllerK2 addTextFieldWithConfigurationHandler:^(UITextField *K2TextField)
{
K2TextField.placeholder = NSLocalizedString(@"Please enter the first number.", @"Please enter the first number.");
}];
[alertControllerK2 addAction:K2okAction];
[self presentViewController:alertControllerK2 animated:YES completion:nil];
} else {
UIAlertView *alertK2;
alertK2 = [[UIAlertView alloc]
initWithTitle:@"\u00A0"
message:@"Please enter the first number."
delegate: self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
alertK2.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertK2 show];
}
The problem is that K2TextField is defined inside the UIAlertController, so I can't access it outside of that code. But if I try to predefine it, I get an error message. Any help would be greatly appreciated!
This is the worst part of using text fields with a UIAlertController. The alert controller returns an array of text fields, and having to access each text field by index is not great when it comes to readability, so it is important to have clear variable names.
We are going to add two text fields to the alert controller. The first one will be to take an email address and the second will be for a password. To add the email text field, add the following code just below where we initialised the UIAlertController: alertController. addTextField { ( textField) in textField. placeholder = "Email" }
The alert controller maintains a reference to each text field so that you can access its value later. The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
The UIAlertController
has an array of textFields
that are ordered by when you added them (the first one you added is index 0). Since it is a generic array, you will have to cast the result before accessing the text
field.
__weak UIAlertController *alertRef = alertController;
UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"Button Text"
handler:^(UIAlertAction * action) {
// access text from text field
NSString *text = ((UITextField *)[alertRef.textFields objectAtIndex:0]).text;
}];
In my case I am re-using the AlertController at various points in the script so in my header .h file I made it global:
UIAlertController *alertController;
And then in my implementation .m file I assign it to the current alert like this:
alertController = (UIAlertController *)self.presentedViewController;
The above retrieves the existing alert and assigns it to the global. For this to work you first need to initialize it or create a new one:
UIAlertController* anyALERTname = [UIAlertController alertControllerWithTitle:@"Alert Title" message:yourAlertMessage preferredStyle:UIAlertControllerStyleAlert];
Now that you have the current AlertController, you can reach out to (and grab) the TextField:
if (alertController) {
//go and get the action field
UITextField *alertText1 = alertController.textFields.firstObject;
NSLog(@"what is alert text? %@",alertText1.text);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With