D. Murphy D. Murphy - 1 month ago 12
Swift Question

Difference Between Prepare For Segue And Prepare for Unwind - Swift 3.0

What are the differences between prepare for segue and prepare for unwind? I have implemented both of these into Xcode project and they both come out with the same result. The code that I have implemented both of these functions are: (with prepare for unwind)

override func prepare(for unwind: UIStoryboardSegue, sender: Any?) {

let variableUnwind = ("StackOverFlow")

if unwind.identifier == "toFirstViewController" {

let hello = unwind.destination as! ViewController

hello.username = textField.text!

print(hello.username)



}

}


and:(with prepare for unwind)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

let variableUnwind = ("StackOverFlow")

if segue.identifier == "toFirstViewController" {

let hello = segue.destination as! ViewController

hello.username = textField.text!

print(hello.username)



}

}

Answer

They are both the same method. It doesn't matter whether the parameter name is unwind or segue, the selector is prepare(for:sender:).

If you try implementing both of them in the same class, you will get the error: invalid redeclaration of 'prepare(for:sender:)' with the second one.

I would suggest using prepare(for segue: UIStoryboardSegue, sender: Any?) because 1) That is what autocomplete suggests, and 2) All segues will go through there, not just unwinding ones. So it makes sense for the parameter to be called segue.

Comments