Reputation: 1102
I have a basic storyboard setup where I load my NavViewController, which then points to initial view controller. I then have several additional view controllers all daisy chained together via segues in a linear fashion. When I initially launch my application i run the following in my NavControllerViewController.m
(void)viewDidLoad
{
[super viewDidLoad];
NSArray * controllerArray = [self viewControllers];
NSLog(@"view controllers: %@", controllerArray);
}
The log only shows the very first root view controller (the one directly 'connected' to the nav controller). All over view controllers are missing from the stack. I was under the impression that if a view controller was on my storyboard that it would automatically be added to the nav controller?
If this is not correct, would a good alternative be to instantiate each VC, from the calling VC? For example, if I wanted to transition from VC1 to the VC2, would I put the following code in VC1:
UIViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"];
[self pushViewController:vc2 animated:YES];
Or possibly:
[self performSegueWithIdentifier:@"vc2Segue" sender:self];
Upvotes: 1
Views: 1386
Reputation: 1072
If I understand your question correctly, you have a storyboard setup looking similar to the below screen shot.When an application loads this storyboard it will definitely have only one view controller in the navigation controller stack and that will be the root view controller.
Because other view controllers are still not pushed into the navigation controller stack
.
In the Viewcontroller-1
, you could see a button some Action
, I have created a push segue
from that button to the Viewcontroller-2
.Once you tap that button, second view controller will be pushed to the navigation controller stack.
If you print the viewcontrollers
count now, you should get the count as 2
.
Repeat the same in the View Controller-2, now you can see the count bumps to 3
. Because now we have three view controllers pushed into the navigation controller stack.
Press the back
button to pop the view controllers and could see the view controllers count coming down, that's because view controllers are now being removed from navigation controller stack.
Upvotes: 1
Reputation: 2383
As AdamG stated, the UIViewController
s will not be pushed on to the stack until you segue to them.
To set a segue, select a UIViewController
and control+drag the connection to the target UIViewController
. Under the Attributes Inspector tab set the Storyboard Segue Identifier.
To segue to a UIViewController
use the method performSegueWithIdentifier:
. Before performSegueWithIdentifier:
is called prepareForSegue:sender:
will be called. This is where you can pass any values that the next UIViewController
needs. To check which segue is being called use the segue.identifier
property in prepareForSegue:sender:
. After that, you can access the destinationViewController
property.
If you need to manually instantiate a UIViewController
use instantiateViewControllerWithIdentifer:
. The identifier can be set under the Identity Inspector tab in the storyboard.
Upvotes: 0
Reputation: 3718
They are all able to be reached by segues, but they are not instantiated or pushed onto the stack until you segue to them.
You could use the self perform segue tactic, or the push view controller, or, alternatively, if it is in response to a single button click, just control drag from that button to the next view controller and Xcode does all the rest of the work for you.
Upvotes: 1