转到导航堆栈中的上一个控制器 (Swift)

Go to previous controller in navigation stack (Swift)

我正在尝试在 alertView 之后将导航堆栈中的上一个视图控制器弹出视图控制器。截至目前,我的程序不会 运行 popViewController 方法,因为 alertView 挡住了路,并引发错误:

    UINavigationController 0x17670900 while an existing transition or presentation is occurring; the navigation stack will not be updated.

在用户从 alertView 中单击“确定”后,我该如何处理 运行popViewController 方法?我是否必须设置一个委托来检测用户何时单击“确定”?

这是我的代码:

    //alertView after Picture saved
    let alertView = UIAlertController(title: "Success!", message: "Record Saved to Database", preferredStyle: .Alert)
    alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))

    self.presentViewController(alertView, animated: true, completion: nil)

    //go to previous controller using popViewController, doesnt work, brings up error message
    if let navController = self.navigationController {
        navController.popViewControllerAnimated(true)
    }

您可以像这样将 "popViewControllerAction" 放入警报操作中。

func alertMethod() {

    var okAlertController = UIAlertController(title: NSLocalizedString("Your title", comment: ""), message: "Your message", preferredStyle: 
    UIAlertControllerStyle.Alert)

    let okAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
        // your action - navController.popViewControllerAnimated(true)
    }

    let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
        // your action
    }

    }

    saveAlertController.addAction(okAction)
    saveAlertController.addAction(cancelAction)
    self.presentViewController(okAlertController, animated: true, completion: nil)
}

这应该可行,在这种情况下,该方法会在用户按下按钮后调用...

当用户从 AlertView 按下 Ok 按钮时,您需要弹出视图控制器。

let alertView = UIAlertController(title: "Success!", message: "Record Saved to Database", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
    // pop here
    if let navController = self.navigationController {
        navController.popViewControllerAnimated(true)
    }
}
alertView.addAction(OKAction)
self.present(alertView, animated: true, completion: nil)