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:
- It instantiates the destination view controller using the attribute values you provided in the storyboard.
- It gives the source view controller an opportunity to configure the new controller.
- It performs the transition configured in the segue.