iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 – 使用UIAlertAction关闭视图控制器全部内容,希望文章能够帮你解决ios – 使用UIAlertAction关闭视图控制器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。