All*_*son 17 input ios8 uialertcontroller
我正在将所有内容从AlertView更改为AlertController,但我无法在线找到任何用于检索用户在AlertController的文本字段中输入内容的内容.这是我有的:
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];
}
Run Code Online (Sandbox Code Playgroud)
问题是K2TextField是在UIAlertController中定义的,所以我无法在该代码之外访问它.但是,如果我尝试预定义它,我会收到一条错误消息.任何帮助将不胜感激!
Bec*_*caP 32
所述UIAlertController具有的阵列textFields是由当加入它们(添加的第一个的索引为0)排序.由于它是通用数组,因此您必须在访问text字段之前强制转换结果.
__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;
}];
Run Code Online (Sandbox Code Playgroud)
就我而言,我在脚本的各个点重新使用 AlertController,因此在我的头文件 .h 中,我将其设为全局:
UIAlertController *alertController;
Run Code Online (Sandbox Code Playgroud)
然后在我的实现 .m 文件中,我将它分配给当前警报,如下所示:
alertController = (UIAlertController *)self.presentedViewController;
Run Code Online (Sandbox Code Playgroud)
以上检索现有警报并将其分配给全局。为此,您首先需要对其进行初始化或创建一个新的:
UIAlertController* anyALERTname = [UIAlertController alertControllerWithTitle:@"Alert Title" message:yourAlertMessage preferredStyle:UIAlertControllerStyleAlert];
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11393 次 |
| 最近记录: |