Ube*_*son 15 ios8 uialertcontroller
我正在尝试使用iOS 8 UIAlertController代替我UIAlertView过去使用过的地方.我希望用户能够在此警报中输入文本并点击"确定"处理文本或"取消"取消.
这是基本代码:
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];
Run Code Online (Sandbox Code Playgroud)
随着老UIAlertView,我会用它标记alertViewStyle的UIAlertViewStylePlainTextInput.然后,如果用户在输入文本后点击其键盘上的"返回"按钮,willDismissWithButtonIndex:则会调用UIAlertViewDelegate方法buttonIndex(取决于在其中指定的按钮UIAlertView).
在新的情况下UIAlertController,如果用户点击"确定"或"取消"按钮,则按预期执行相应的操作; 但如果用户只是按下键盘上的"返回"键,它只是隐藏了键盘,但警报仍保留在屏幕上,并且不执行任何操作.
我已经考虑过配置文本字段来设置UITextFieldDelegateto self,然后可能重写textFieldDidReturn:方法,但我也不知道是否有办法以UIAlertController编程方式调用其中一个动作.无论如何,这听起来有点混乱/ hacky.我错过了一些明显的东西吗
在iOS 9.3中,当您有多个带UIAlertActionStyleDefault样式的操作时会发生这种情况.
当您添加一个动作UIAlertActionStyleDefault和另一个UIAlertActionStyleCancel动作时,将调用具有默认样式的动作
| 归档时间: |
|
| 查看次数: |
8870 次 |
| 最近记录: |