How do I access the input from my text field in UIAlertController with objective c?

后端 未结 2 1811
旧时难觅i
旧时难觅i 2020-12-31 03:47

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 th

相关标签:
2条回答
  • 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;
                                             }];
    
    0 讨论(0)
  • 2020-12-31 04:14

    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);
    
    
    }
    
    0 讨论(0)
提交回复
热议问题