【问题标题】:Dismiss a View Controller with a UIAlertAction使用 UIAlertAction 关闭视图控制器
【发布时间】:2016-06-07 05:20:15
【问题描述】:

我正在尝试显示注销警报。当用户点击 Yes 时,我希望我的视图控制器使用可以为我提供完成处理程序的方法来关闭。

视图控制器位于导航控制器内,是堆栈中的第二个。

我想出了以下代码:

@IBAction func logOut() {
        let logOutAlert = UIAlertController.init(title: "Log out", message: "Are you sure ?", preferredStyle:.Alert)

        logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
            //Present entry view ==> NOT EXECUTED
            self.dismissViewControllerAnimated(true, completion:nil)
        })

        logOutAlert.addAction(UIAlertAction.init(title: "Cancel", style: .Cancel, handler: nil))

        self.presentViewController(logOutAlert, animated: true, completion: nil)
}

self.dismissViewControllerAnimated(true, completion:nil) 行已读取,但没有执行任何操作。

【问题讨论】:

    标签: ios swift uialertview dismiss


    【解决方案1】:

    我怀疑dismissViewControllerAnimated 不会为您做任何事情,因为视图控制器不是模态显示的,而是通过导航控制器显示的。要关闭它,您可以告诉导航控制器将其从堆栈中弹出,如下所示:

        logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
            self.navigationController?.popViewControllerAnimated(true)
            })
    

    不幸的是,popViewControllerAnimated 似乎没有提供开箱即用的方法来附加您自己的完成处理程序。如果您需要一个,您仍然可以通过使用关联的CATransaction 添加一个,它可能看起来像这样:

        logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
            CATransaction.begin()
            CATransaction.setCompletionBlock(/* YOUR BLOCK GOES HERE */)
            self.navigationController?.popViewControllerAnimated(true)
            CATransaction.commit()
            })
    

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2013-01-01
      相关资源
      最近更新 更多