gpt4 book ai didi

ios8 - 带有文本字段的 UIAlertController - 点击返回按钮仅隐藏键盘,不执行操作?

转载 作者:行者123 更新时间:2023-12-03 20:52:33 25 4
gpt4 key购买 nike

我正在尝试使用 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];

同老 UIAlertView ,我会用 alertViewStyle 标记它的 UIAlertViewStylePlainTextInput .然后,如果用户在输入文本后点击键盘上的“返回”按钮,UIAlertViewDelegate 方法 willDismissWithButtonIndex:会被一些 buttonIndex 调用(取决于在 UIAlertView 中指定了哪些按钮)。

中新 UIAlertController , 如果用户点击“确定”或“取消”按钮,则它们的相应 Action 按预期执行;但是如果用户只是点击键盘上的“返回”键,它只会隐藏键盘,但警报仍然在屏幕上并且不会执行任何操作。

我想过配置文本字段来设置 UITextFieldDelegateself ,然后可能会覆盖 textFieldDidReturn:方法,但我也不知道是否有办法调用 UIAlertController 之一以编程方式执行的操作。无论如何,这听起来有点困惑/hacky。我错过了一些明显的东西吗?

最佳答案

在 iOS 9.3 上,当您使用 UIAlertActionStyleDefault 执行多个操作时会发生这种情况。风格。

当您使用 UIAlertActionStyleDefault 添加一项操作时另一个是 UIAlertActionStyleCancel , 将调用具有默认样式的操作

关于ios8 - 带有文本字段的 UIAlertController - 点击返回按钮仅隐藏键盘,不执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com