Amb*_*lon 4 uialertview dismiss ios swift
我正在尝试提示退出警报.当用户点击是时,我希望我的视图控制器使用可以为我提供完成处理程序的方法来解除.
视图控制器位于导航控制器内,是堆栈中的第二个.
我想出了以下代码:
@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)
}
Run Code Online (Sandbox Code Playgroud)
该行self.dismissViewControllerAnimated(true, completion:nil)被读取但它没有做任何事情.
我怀疑这dismissViewControllerAnimated对你没有任何作用,因为视图控制器没有以模态方式呈现,而是通过导航控制器显示.要解雇是,您可以告诉导航控制器从堆栈中弹出它,如下所示:
logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
self.navigationController?.popViewControllerAnimated(true)
})
Run Code Online (Sandbox Code Playgroud)
不幸的是,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()
})
Run Code Online (Sandbox Code Playgroud)