[英]Go to previous controller in navigation stack (Swift)
我試圖在alertView之后將popViewController彈出到導航堆棧中的上一個View Controller。 到目前為止,由於alertView妨礙了我的程序,因此不會運行popViewController方法,並顯示錯誤消息:
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)
}
當用戶從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)
您可以將“ 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)
}
那應該起作用,在這種情況下,用戶按下按鈕后便會調用該方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.