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

时间:2015-07-12 08:18:45

标签: ios iphone swift uialertview

我正在尝试在alertView之后将popViewController导航到导航堆栈中的上一个View Controller。截至目前,我的程序将不会运行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)
    }

2 个答案:

答案 0 :(得分:5)

当用户从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)

答案 1 :(得分:1)

你可以把" 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)
}

这应该有效,在这种情况下,该方法在用户按下按钮后被调用...