简体   繁体   English

如何从UIAlertcontroller中消除模式ViewController

[英]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.

相关问题 如何从右到左关闭模态ViewController? - How to dismiss modal ViewController from right to left? 将2模式呈现中的viewController撤消到特定的viewController - Dismiss a viewController from 2 modal presentation to especific viewController 如何在不关闭模式的情况下关闭从modalViewController推送的viewController? - How to dismiss a viewController pushed from a modalViewController without dismissing the modal? 从推出的ViewController内部中删除Modal NavigationController - Dismiss Modal NavigationController from pushed ViewController inside 如何正确解除带完成处理程序的模态viewcontroller - How to properly dismiss modal viewcontroller with completion handler 如何完全从视图中解脱uialertcontroller? - How to dismiss uialertcontroller from view completely? 关闭模态ViewController弹出窗口 - Dismiss Modal ViewController Popup 在 UIAlertController 外部点击时如何关闭 UIAlertController? - How to dismiss UIAlertController when tap outside the UIAlertController? 解雇由模态视图控制器呈现的UIAlertController - dismiss UIAlertController presented by a modal view controller 在Master上点击新项目时,从Detail viewController中删除模态 - Dismiss a modal from Detail viewController when tapping new item on Master
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM