目标C – 我想在单击按钮时不删除UIAlertController

我想提出一个带有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属性。 它运作良好。