Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between perform segue with identifier and prepare for segue

I am very confuse between "perform segue with identifier" and "prepare for segue"...what these functions do and how they work?

like image 318
Uzair Avatar asked Aug 07 '15 12:08

Uzair


4 Answers

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

like image 192
Nerkyator Avatar answered Oct 19 '22 00:10

Nerkyator


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.

like image 44
Dharmesh Kheni Avatar answered Oct 18 '22 23:10

Dharmesh Kheni


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];
    }
}
like image 5
Adrian Avatar answered Oct 18 '22 22:10

Adrian


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.

like image 2
sylvia Avatar answered Oct 18 '22 22:10

sylvia