2015-07-12 4 views
2

Я пытаюсь popViewController на предыдущий контроллер просмотра в стеке навигации после alertView. В настоящее время, моя программа не будет работать метод 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. Он будет вызываться, когда пользователь нажимает кнопку OK. –

ответ

5

Вы должны совать контроллер представления, когда пользователь нажимает кнопка Ok от AlertView.

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» внутри предупреждения действий, как это.

функ 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) 
} 

Это должно работать, в этом случае метод вызывается после того, как пользователь нажимает кнопку ...

Смежные вопросы