ios – 使用UIAlertAction关闭视图控制器

前端之家收集整理的这篇文章主要介绍了ios – 使用UIAlertAction关闭视图控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试提示退出警报.当用户点击是时,我希望我的视图控制器使用可以为我提供完成处理程序的方法来解除.

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

我想出了以下代码

@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)行被读取,但它没有做任何事情.

解决方法

我怀疑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()
        })

猜你在找的iOS相关文章