关闭由模态视图控制器呈现的UIAlertController

7

我似乎遇到了一个类似于未解决问题的问题:UIAlertController dismissing his presentingViewController

我在一个普通的UIViewController上呈现了一个模态视图控制器。然后我在该模态视图控制器上弹出了一个警告框。当我点击“确定”以关闭警告框(使用下面的代码生成),模态视图控制器也被关闭。

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK"
                                             style:UIAlertActionStyleDefault 
                                           handler:^(UIAlertAction *action{ 
                                                    [self dismissViewControllerAnimated: YES completion: nil];}];

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sign up problem."
                                                               message:@"Some fields are empty. Please check your inputs and try again."
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];

我怎样才能仅关闭警告框?

我意识到可以通过使用导航控制器类型的设置来避免这个问题,并隐藏导航栏,从而获得与模态视图控制器相同的感觉,但这似乎很愚蠢。谢谢。

2个回答

23

在按钮处理程序中不要调用 self dismissViewController。这明确表示您要解散视图控制器。

您不需要解散警报。它会自动解散自己。您在按钮处理程序中应该做的唯一的事情是执行所需的任何操作。如果您不需要执行任何操作,则不要执行任何操作。

如果您的警报只是一条消息,并且您不需要执行任何操作,只需执行此操作:

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

非常感谢,实际上这两种行为都是有用的。 - Sunnyside Productions

0

在按钮处理程序中,您不需要手动解除或删除UIAlertController - 它会自行完成。

只需删除对dismissViewControllerAnimated:completion:的调用即可。


但如果你真的想出于特定原因这样做呢?肯定有一种以编程方式来解除它的方法。例如,在旧版的UIAlertView中,你可以以编程方式来解除/隐藏它。 - Supertecnoboff
@Supertechnoboff:我认为你可以直接使用[self dismissViewControllerAnimated:completion:]。不过我不确定。 - Linuxios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接