2015-07-12 67 views
2

我试图在alertView后将popViewController导航到导航堆栈中的前一个视图控制器。截至目前,我的程序将无法运行popViewController方法,因为alertView是在路上,并提出了错误:转到导航堆栈中的前一个控制器(Swift)

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

我该如何去对用户运行后popViewController方法点击从OK alertView?我是否必须设置一个代表,用于检测何时使用的点击确定?

这里是我的代码:

//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) 
    } 
+1

您正在同时调用两个转换:当前警报和弹出前一个视图控制器。 像现在这样调用当前警报,但在动作处理程序中调用popViewController。当用户点击确定按钮时,它将被调用。 –

回答

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

谢谢Yogesh。 –

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) 
} 

这应该工作,在这种情况下,用户按下一个按钮后,该方法被调用......

相关问题