【问题标题】:How to dismiss a UIViewController from a UIAlertController's UIAlertAction handler?如何从 UIAlertController 的 UIAlertAction 处理程序中关闭 UIViewController?
【发布时间】:2016-03-04 22:36:16
【问题描述】:

我想向用户展示一个简单的 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),但这也不起作用。

【问题讨论】:

    标签: swift uiviewcontroller uialertcontroller


    【解决方案1】:

    使用

    self.navigationController?.popViewControllerAnimated(true)
    

    而不是

    self.dismissViewControllerAnimated(true, completion: nil)
    

    按要求工作并关闭显示警报的视图控制器。

    【讨论】:

      【解决方案2】:

      您使用 presentViewController 来显示警报。因此,如果您使用 self.dismissViewController,它只会关闭警报,而不是父视图控制器。如果您尝试隐藏父视图控制器,您可以创建一个宽度高度等于 self.view 的视图并将其设置为黑色。然后使用 view.addSubView 随时添加。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-03
        • 1970-01-01
        • 2014-10-17
        相关资源
        最近更新 更多