繁体   English   中英

如何从UIAlertcontroller中消除模式ViewController

[英]How to dismiss modal ViewController from UIAlertcontroller

我展示了一个模态viewcontroller ,用户可以在其上决定编辑或删除所展示的汽车。

如果用户想删除这辆车,我会提供一个具有警报样式的UIAlertController ,询问他是否真的要删除这辆车。 一切正常。 但是在用户选择“ ”之后,我仍然处于modal viewcontroller

删除后如何关闭模态视图?

我尝试了以下代码

self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)

self.navigationController?.popViewControllerAnimated(true)

在“确定行动”的结局中,但两者都不对我有用。 :(

没有理由在其中编写代码

dispatch_async(dispatch_get_main_queue(), {    //write your code here

 })

上面的代码用于在主线程中工作。 但是这里您已经在主线程上了。

这里的问题是你在打电话

self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)

而不是写

self.dismissViewControllerAnimated(true, completion: nil)

因为您要在self控制器上显示AlertController ,所以唯一可以将其关闭的人是self

将您的代码放入

 dispatch_async(dispatch_get_main_queue(), {    //write your code here

 })

像那样 :

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)
     })

}

希望这对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM