10

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Uzair
  • 231
  • 4
  • 18

6 Answers6

10

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
5

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];
    }
}
Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • I have a TextField in "SecondViewController (YourViewController)" and I want to change frame and Placeholder text. How can I do it? any help please. – Ganesh G Feb 20 '17 at 12:41
4

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

Nerkyator
  • 3,976
  • 1
  • 25
  • 31
2

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.

sylvia
  • 65
  • 5
0

Perform segue, actually changes the view that the user sees. The perform segue function will call the prepare segue function before it changes the view; so, generally, developers will pass information to the next controller within that function before it segues.

0

Swift 3 syntax is: performSegue(withIdentifier: String, sender: Any?). You can do this if you just want to bring the viewController without any pre-configuration or conditons

You can also do prepare(for: NSStoryboardSegue, sender: Any?) which gives extra features:

  • Passing extra data to the viewController
  • doing an if else statement so that based switch between e.g. passing login screen or register screen

basically : prepareForSegue prepares data to passed between view controllers where as performSegue actually allows the switch to happen

mfaani
  • 33,269
  • 19
  • 164
  • 293