当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - How to unwind segue with alert view in Swift?

问题描述:

I have several View Controllers. I need to return to the first View Controller if the Alert View is confirmed. This is how I would do it without the unwind Segue:

@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)

}

But if I do that, in this code it crashes because of the last line of code.

This is the error:

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.

How can I complete the Alert View while being able to unwind segue.

Thank you

网友答案:

You are putting up the alert after the unwind has happened. You want to be able to not perform the unwind at all if the user chooses to cancel the delete. I would suggest the following:

  1. Instead of wiring the unwind segue to your delete button, instead connect it to the view controller icon at the top of the view controller so that you can call the unwind programmatically. This answer shows you how to do that: Setting up the unwind segue. Give the unwind segue an identifier such as "doUnwind".
  2. In the @IBAction for your delete button, put up the alert asking the user if they really want to delete.

  3. In the handler for the delete button, call the unwind segue programmatically.

        @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)
    
        }
    
分享给朋友:
您可能感兴趣的文章:
随机阅读: