I am very confuse between "perform segue with identifier" and "prepare for segue"...what these functions do and how they work?
performSegueWithIdentifier as it says, makes a transition from your current UIViewController
to segue-connected UIViewController
prepareForSegue let you to do some additional works before transition happens
prepareForSegue prepares data to passed between view controllers where as performSegue with identifier actually allows the switch to happen.
From Apple Documentation:
Swift2:
performSegueWithIdentifier(_:sender:)
Swift3:
performSegue(withIdentifier: String, sender: Any?)
Apps typically do not need to trigger segues programmatically. If needed, you can call this method to trigger a segue for an action that cannot be expressed in a storyboard file, such as a transition between scenes in different storyboards.
Typically, a segue is triggered by a user action, such as clicking a button. In Interface Builder, configure an object, such as a control embedded in the view controller’s view hierarchy, to trigger the segue.
Swift2:
prepareForSegue(_:sender:)
Swift3:
prepare(for: NSStoryboardSegue, sender: Any?)
The default implementation of this method does nothing; you can override it to pass relevant data to the new view controller or window controller, based on the context of the segue. The segue object describes the transition and includes references to both controllers involved in the segue.
Segues can be triggered from multiple sources, so use the information in the segue and sender parameters to disambiguate between different logical paths in your app. For example, if the segue originated from a table view, the sender parameter would identify the cell that the user clicked. You could use that information to set the data on the destination view controller.
performSegueWithIdentifier
just tells the viewController what segue you'd like segue to. You can give your segues names in Interface Builder.
[self performSegueWithIdentifier:@"GoToAnotherViewController" sender:self];
You may have a ViewController with several segues or you may have something from the current ViewController the destinationVC "needs to know" when you segue, so you'd use prepareForSegue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"GoToAnotherViewController"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
There is a difference that I didn't see in the answers here and may help to clarify.
The performSegueWithIdentifier empowers your code while the prepareForSegue empowers the user, needs an user action.
Let's say you have a quiz with some questions and the user has 30 seconds to answer each of them.
When the user clicks one of the answers the prepareForSegue will be called. The user is in charge here.
In the other hand, if the user doesn't click anything but the 30 seconds expire, then YOUR CODE should 'perform the segue' automatically.
That's when you need the performSegueWithIdentifier to do the job programatically. Your code is directly in charge here.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With