I am wondering about how the functions in the title work and also about the sender parameter.
Lets say a button click calls the performSegue method, does that also call the prepareSegue method as well? Is the prepareSegue method called before the performSegue method but after the button is pressed?
Also, is the "sender" parameter in both of the functions linked? If I pass in a string as the sender in the performSegue method, will that transfer over to the sender parameter in the prepareSegue method? In other words, if I set the sender parameter in the performSegue method as "Hi world", will the sender parameter in the prepareSegue method also be the same string?
There are, effectively, two ways you can trigger a segue. The first is via an action on a UI element in Interface Builder, the second is using
performSegueWithIdentifier:sender: in your code. I say 'effectively', because under the covers, when the scene is loaded from the storyboard, an action handler is configured that ultimately calls
performSegueWithIdentifier:sender: is called, the segue object is delivered to your view controller's
In the case where the segue was initiated by an action on a UI element then the sender will be that UI element (i.e. if it is an action connection on a
UIButton then the
sender will be the
If the segue is initiated by your code calling
performSegueWithIdentifier:sender: then the
sender will be whatever object you passed as the
sender. This could be your view controller, a button, an array, anything. So yes, if you pass "Hello World" to
performSegueWithIdentifier:sender: as the
sender value then this will be the
In terms of the order of operations:
performSegueWithIdentifier:senderis called, either by your code or as a result of an action on a UI element
shouldPerformSegueWithIdentifier:sender:then this function is called. If this function returns
falsethen the segue is cancelled
prepareForSegue:sender:then this function is called.
prepareForSegue:sender:returns, the segue completes.