如何在Swift中使用警报视图展开segue?

时间:2015-04-30 05:22:24

标签: iphone swift uialertview unwind-segue

我有几个视图控制器。如果确认了警报视图,我需要返回第一个View Controller。如果没有放松塞格,我就会这样做:

@IBAction func unwindToDelete ( segue: UIStoryboardSegue ) {

    let alertView = UIAlertController(title: "Delete?", message: "Are you sure you wante to delete?", preferredStyle: .ActionSheet)

    let deleteAction = UIAlertAction (title: "Delete", style: .Destructive ) { alertAction in

        self.deleteChoice = true
    }

    let cancelAction = UIAlertAction (title: "Cancel", style: .Cancel ) {  alertAction in

    }

    alertView.addAction(deleteAction)
    alertView.addAction(cancelAction)

    self.presentViewController(alertView, animated: true, completion: nil)

}

但是,如果我这样做,在这段代码中它会崩溃,因为最后一行代码。

这是错误:

2015-04-30 14:59:45.605 PhotosCollection[4624:182995] 
popToViewController:transition: called on <UINavigationController 0x7a67aeb0> 
while an existing transition or presentation is occurring; the navigation 
stack  will not be updated. 

如何在能够解开segue的同时完成警报视图。

谢谢

1 个答案:

答案 0 :(得分:8)

放松后你正在发出警报。如果用户选择取消删除,您希望根本不能执行展开。我建议如下:

  1. 而不是将展开segue连接到删除按钮,而是将其连接到视图控制器顶部的视图控制器图标,以便您可以以编程方式调用展开。这个答案告诉你如何做到这一点: Setting up the unwind segue。 给展开segue一个标识符,例如"doUnwind"
  2. 在删除按钮的@IBAction中,提出警告,询问用户是否确实要删除。

  3. 在删除按钮的处理程序中,以编程方式调用unwind segue。

        @IBAction func deleteButton (button: UIButton) {
    
            let alertView = UIAlertController(title: "Delete?", message: "Are you sure you wante to delete?", preferredStyle: .ActionSheet)
    
            let deleteAction = UIAlertAction (title: "Delete", style: .Destructive ) { alertAction in
    
                 self.performSegueWithIdentifier("doUnwind", sender: self)
            }
    
            let cancelAction = UIAlertAction (title: "Cancel", style: .Cancel ) {  alertAction in
    
            }
    
            alertView.addAction(deleteAction)
            alertView.addAction(cancelAction)
    
            self.presentViewController(alertView, animated: true, completion: nil)
    
        }