如何在UIAlertAction处理程序中从UIAlertController中解除UIViewController?

13

我想向用户展示一个简单的UIAlertController,其中一个选项会触发父视图控制器关闭。这是我正在使用的代码:

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Close View", style: UIAlertActionStyle.Destructive, handler: {
        action in

        self.dismissViewControllerAnimated(true, completion: nil)
    }))

    alert.addAction(UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)

它没有任何效果。执行“关闭视图”处理程序后,呈现警报的视图控制器仍然存在。

我还尝试在UIAlertAction操作块中使用self.navigationController?.dismissViewControllerAnimated(true, completion: nil),但那也不起作用。

2个回答

17

使用

self.navigationController?.popViewControllerAnimated(true)

取代

self.dismissViewControllerAnimated(true, completion: nil)

正常工作并关闭显示警报的视图控制器。


0
你可以使用presentViewController来展示警告。因此,如果你使用self.dismissViewController,它只会关闭警告,而不是父视图控制器。如果你想要隐藏父视图控制器,你可以创建一个宽高与self.view相等、颜色为黑色的视图。然后在任何你想要的时候使用view.addSubView将其添加进去。

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