-2

Why does the following code from Passing Data between View Controllers work ? :

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

     if([segue.identifier isEqualToString:@"showDetailSegue"])
     {
         ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
         controller.isSomethingEnabled = YES;
     }

}

Everything that I see is that he creates an instance of the ViewControllerB class . I don't understand how that instance (controller) can be the actual ViewController to which it is passing that data? What concept am I missing?

Community
  • 1
  • 1
Bogdan Pop
  • 318
  • 1
  • 3
  • 12

1 Answers1

2

The code you posted is not instantiating a new ViewControllerB instance. By the time prepareForSegue:sender: is called, the view controller has already been instantiated by the storyboard and assigned to the passed-in segue's destinationViewController property. So the code is, in fact, just getting a reference to the view controller that has already been created and will be used in the segue.

See the section Segues Automatically Instantiate the Destination View Controller in the View Controller Programming Guide for iOS:

A segue represents a triggered transition that brings a new view controller into your app’s user interface.

Segues contain a lot of information about the transition, including the following:

  • The object that caused the segue to be triggered, known as the sender
  • The source view controller that starts the segue
  • The destination view controller to be instantiated
  • The kind of transition that should be used to bring the destination view controller onscreen
  • An optional identifier string that identifies that specific segue in the storyboard

When a segue is triggered, iOS takes the following actions:

  1. It instantiates the destination view controller using the attribute values you provided in the storyboard.
  2. It gives the source view controller an opportunity to configure the new controller.
  3. It performs the transition configured in the segue.
Stuart
  • 36,683
  • 19
  • 101
  • 139
  • Oh, I think I got it. When creating that controller , I will assign to it the memory adress of segue.destinationViewController which is a reference to the actual ViewController which I am passing data to, so controller will actually be that ViewController (because it points to the same address as segue.destinationViewController) , right? – Bogdan Pop Apr 12 '15 at 11:25
  • When performing segues that are created in a storyboard (as is the case ~99% of the time), you don't create _or_ assign the view controller at all - it is done for you. You just make use of `prepareForSegue:sender:` to configure ("prepare") that view controller. To facilitate this, a reference to the view controller is accessible to you through the passed-in `segue` (its `destinationViewController` property). So in that sense you're right - `segue.destinationViewController` points to the same memory address as the actual view controller being transitioned to. – Stuart Apr 12 '15 at 13:12
  • ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController; controller will point to the address of segue.destinationViewController ? I'm asking this because I only modify controller.isSomethingEnabled, not segue.destinationViewController.isSomethingEnabled – Bogdan Pop Apr 12 '15 at 13:19
  • Yes. Objects in Objective-C are allocated on the heap and referenced by pointers; you are just passing a pointer reference to a different variable (`controller`). Your question hints that you would benefit from reading ["Working with Objects" in Programming with Objective-C](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html). – Stuart Apr 12 '15 at 13:27