目标C - 我希望在单击按钮时不会删除UIAlert控制器



我想展示一个带有 2 个按钮的 UIAlertController。

一个按钮应关闭警报,第二个按钮应执行操作,但警报仍保留在屏幕上。是否可以对不会关闭警报的操作进行一些配置?

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
message:@"message"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Do Something"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//Pressing this button, should not remove alert from screen
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Close"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//Regular functionality- pressing removes alert from screen 
}]];

[alert show];

这(防止UIAlertController关闭(被建议作为可能的答案,但问题涉及文本字段。我需要其中一个操作按钮在按下时不关闭警报。

你不能这样做。

只有一个解决方案是创建一个看起来像本机UIAlertController的自定义视图控制器。

你不能用默认的UIAlertViewController来做到这一点,如果你想这样做,你需要创建自定义的视图控制器,其看起来像UIAlertController
您可以使用此自定义视图控制器。

https://github.com/nealyoung/NYAlertViewController

从用户的角度来看,按下按钮而不执行操作会让我怀疑是否有东西被破坏了。如果您试图以此为契机获取更多信息,或有关按钮按下意图的一些详细信息,我认为遵循用户期望的解决方案(尽管可能有点烦人?(是在关闭第一个对话框后简单地显示第二个对话框。基本上,这是一种"每个问题一个对话框"的处理方式。

否则,我会同意其他建议,并说您需要在此处的自定义视图,而不是库存的UIKit解决方案。

试试这段代码。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
message:@"message"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Do Something"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"Do Something Tapped");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Close"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"Close Tapped");
}]];

[self presentViewController:alert animated:YES completion:nil];

这段代码怎么样:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title"
message:@"message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *doSomethingAction = [UIAlertAction actionWithTitle:@"Do Something"
style:UIAlertActionStyleDefault
handler:nil];
doSomethingAction.enabled = NO;
[alert addAction:doSomethingAction];
[alert addAction:[UIAlertAction actionWithTitle:@"Close"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//Regular functionality- pressing removes alert from screen
}]];
[self presentViewController:alert animated:true completion:nil];

NO设置为 UIAlertAction 的enabled属性。效果很好。

最新更新