我试图使用iOS 8 UIAlertController
到位的,我会用过去一个UIAlertView
。我希望用户能够将文本输入到此警报中,并点击“确定”处理文本或“取消”以取消。带文本字段的UIAlertController - 点击返回按钮只隐藏键盘,不执行操作?
这里的基本代码:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Change Value" message:@"Enter your new value."];
[alert addTextFieldWithConfigurationHandler:nil];
[alert addAction: [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *textField = alert.textFields[0];
NSLog(@"text was %@", textField.text);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"Cancel pressed");
}]];
[presentingVC presentViewController:alert animated:YES completion:nil];
在旧UIAlertView
,我会的UIAlertViewStylePlainTextInput
的alertViewStyle
标记它。然后,如果用户打输入文本后,他们的键盘上的“返回”按钮,UIAlertViewDelegate方法willDismissWithButtonIndex:
将与一些buttonIndex
(具体取决于UIAlertView
已经指定哪些按钮)调用。
在新UIAlertController
,如果用户点击“确定”或“取消”按钮,然后按预期的方式执行其相应的操作;但是如果用户刚刚击中键盘上的“返回”键,它只是隐藏键盘,但警报仍保留在屏幕上,并且不执行任何操作。
我想过配置的文本字段设置UITextFieldDelegate
到self
,然后可能重写textFieldDidReturn:
方法,但我也不知道,如果有一种方法来调用UIAlertController
的行动程序之一。无论如何,这听起来有点乱。我错过了明显的东西吗?
我找不到以编程方式触发动作的方法。多么疏忽... – elsurudo 2016-02-05 17:36:17