2015-07-12 86 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) 
} 

這應該工作,在這種情況下,用戶按下一個按鈕後,該方法被調用......

相關問題