如何使用目标c从UIAlertController中的文本字段访问输入? [英] How do I access the input from my text field in UIAlertController with objective c?

查看:97
本文介绍了如何使用目标c从UIAlertController中的文本字段访问输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将所有内容从AlertView更改为AlertController,但我无法在线找到任何用于检索用户在AlertController的文本字段中输入内容的内容。以下是我的内容:

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];
        }

问题是K2TextField是在UIAlertController中定义的,所以我不能在该代码之外访问它。但是,如果我尝试预定义它,我会收到一条错误消息。任何帮助将不胜感激!

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!

推荐答案

UIAlertController 有一个数组添加它们时排序的 textFields (您添加的第一个是索引0)。由于它是通用数组,因此您必须在访问 text 字段之前强制转换结果。

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;
                                         }];

这篇关于如何使用目标c从UIAlertController中的文本字段访问输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆