[英]How to dismiss modal ViewController from UIAlertcontroller
I show a modal viewcontroller
on which the user can decide to edit oder delete the presented car. 我展示了一个模态
viewcontroller
,用户可以在其上决定编辑或删除所展示的汽车。
If the user wants to delete this car I present an UIAlertController
with alert style to ask if he really wants to delete this car. 如果用户想删除这辆车,我会提供一个具有警报样式的
UIAlertController
,询问他是否真的要删除这辆车。 Everything works fine. 一切正常。 But after the user chooses " Yes " I am still in the
modal viewcontroller
. 但是在用户选择“ 是 ”之后,我仍然处于
modal viewcontroller
。
How can I dismiss the modal view after the deletion? 删除后如何关闭模态视图?
I tried Following Code 我尝试了以下代码
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
and 和
self.navigationController?.popViewControllerAnimated(true)
in the closure of the ok Action but both didn't worked for me. 在“确定行动”的结局中,但两者都不对我有用。 :(
:(
There is no reason to write the code in 没有理由在其中编写代码
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
The above code is used to the work in main thread. 上面的代码用于在主线程中工作。 But here you are already on the main thread.
但是这里您已经在主线程上了。
The issue here is you are calling 这里的问题是你在打电话
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
Instead of it just write 而不是写
self.dismissViewControllerAnimated(true, completion: nil)
Because you are presenting AlertController
on self
controller so the only one who can dismiss it is self
因为您要在
self
控制器上显示AlertController
,所以唯一可以将其关闭的人是self
Put your code inside the 将您的代码放入
dispatch_async(dispatch_get_main_queue(), { //write your code here
})
like that : 像那样 :
func showDeleteWarningAndDeleteEntity() {
dispatch_async(dispatch_get_main_queue(), {
let deleteController = UIAlertController(title: "Delete car", message: "Are you sure?", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {
(action) in
}
let okACtion = UIAlertAction(title: "Yes", style: .Destructive) {
(action) in
//some network stuff happens here...
self.dismissViewControllerAnimated(true, completion: nil)
}
deleteController.addAction(okACtion)
deleteController.addAction(cancelAction)
self.presentViewController(deleteController, animated: true, completion: nil)
})
}
Hope this will works for you 希望这对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.