0
votes

I have a Tabbarcontroller filled with 5 Viewcontrollers and Navigationcontrollers as I did here:

[self addChildViewController:VC1];
[self addChildViewController:NavigationController;
[self addChildViewController:VC2];
[self addChildViewController:VC3];
[self addChildViewController:VC4];

Now the thing is, that pressing a button on my Tabbar gets me to every ViewController easily, where I can present Xib-Files etc.

But now I want to have a Navigationcontroller, which is shown when pressing a button on my Tabbar. This Navigationcontroller itself has several Viewcontrollers.

I tried this to present my first Viewcontroller inside my Navigationcontroller (this code is from the Navigationcontroller.m):

- (void)viewDidLoad {
[super viewDidLoad];

[self addChildViewController:VC5];
[self presentViewController:VC5];

}

This expectedly did not work and gave me: Application tried to present modally an active controller.

Is there a good way to achieve such a specific goal? I'm struggling with this problem. Thanks in advance!

edit: This is how I set it up in my storyboard. In my programmatic approach the first view controller is not shown.

enter image description here

1
it says u r trying to present VC5 on VC5. - Teja Nandamuri
You need to be more specific. Are you trying to add a VC to your navigation stack ? - Teja Nandamuri
Maybe I am going the wrong way. I will show you a Screenshot of my Storyboard in which i achieved this goal easily. But now I want to set it up programmatically. - Vancore
Sorry didnt read your comment @TejaNandamuri, yes I am trying to build a navigation stack out of View Controllers, and this stack should be accessible by a tabbar item for the user. - Vancore

1 Answers

1
votes

Instead of adding the VC5 view controller to the NavigationController as a child (unless it's meant to be a child?) add it as the root view controller when you add the NavigationController to the tab bar.

For example in your tab bar code:

[self addChildViewController:VC1];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:VC5]; 
[self addChildViewController:navigationController];
[self addChildViewController:VC2];
[self addChildViewController:VC3];
[self addChildViewController:VC4];

Apple docs on UINavigationController are here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/initWithRootViewController: