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

时间:2016-01-04 17:50:11

标签: 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: {})
    }

在按钮的IB动作功能中,我有一个警告,然后是一些代码,以便更改为另一个ViewController。

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

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

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

1 个答案:

答案 0 :(得分: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)
}

注意:我不能流利使用Swift,因此语法可能会稍微偏离。

相关问题