当警报被解除时,如何弹出RootViewController?

use*_*936 3 ios swift uialertcontroller

@IBAction func addButton(sender: AnyObject) {

    let alert = UIAlertController(title: "New Exercise Added", message: "\(name)", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok!!", style: UIAlertActionStyle.Default, handler: nil))

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

    self.navigationController?.popToRootViewControllerAnimated(true)
    self.dismissViewControllerAnimated(true, completion: {})
    }
Run Code Online (Sandbox Code Playgroud)

在按钮的IB动作功能中,我有一个警报,然后是一些代码,以更改为不同的ViewController.

在警报后到达这些代码行时程序崩溃:

2016-01-04 17:48:27.147 FitnessApp [60584:4080964] popToViewController:transition:在发生现有转换或演示时调用; 导航堆栈不会更新.

转换完成后如何运行代码来更改ViewController?

rma*_*ddy 8

您最大的问题是您没有对警报按钮的处理程序执行任何操作.相反,您在发出警报后立即尝试执行弹出和解除.

移动代码以将控制器弹出到Ok按钮的警报处理程序中.

@IBAction func addButton(sender: AnyObject) {
    let alert = UIAlertController(title: "New Exercise Added", message: "\(name)", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok!!", style: UIAlertActionStyle.Default, handler: {
        self.navigationController?.popToRootViewControllerAnimated(true)
        // You only need the pop
        //self.dismissViewControllerAnimated(true, completion: {})
    }))

    self.presentViewController(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

注意:我不熟悉Swift,所以语法可能会有点偏差.