presentViewController在shouldPerformSegueWithIdentifier中不是模态的

时间:2016-02-04 19:34:39

标签: ios

我正在尝试解开segue取决于用户在警告对话框中批准它。但是,当我运行代码时,对话框会快速出现,消失,然后转移到另一个视图控制器。我已经尝试将alertDialog作为类的实例变量,这没有什么区别。下面的代码位于UIViewController子类中。我错过了什么?

感谢。

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    var shouldPerformSeque = true

    if identifier == "startOver" {
        let alertDialog = UIAlertController(title: "Warning",
            message: "This will discard all data entered.",
            preferredStyle: .Alert)

        let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertDialog.addAction(okAction)

        let cancelActionHandler = {
            (action: UIAlertAction!) -> Void in
            shouldPerformSeque = false
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel,
            handler: cancelActionHandler)
        alertDialog.addAction(cancelAction)

        presentViewController(alertDialog, animated: false, completion: nil)
    }

    return shouldPerformSeque
}

1 个答案:

答案 0 :(得分:0)

您无法在shouldPerformSegueWithIndentifier中执行此操作,因为对话框将以异步方式显示该方法;当动作处理程序关闭执行时,该方法已经返回。

您需要显示对话框以响应触发segue的任何内容,然后根据用户的响应以编程方式执行展开。