0

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

Vancore
  • 657
  • 1
  • 9
  • 18
  • it says u r trying to present VC5 on VC5. – Teja Nandamuri Mar 03 '16 at 15:12
  • You need to be more specific. Are you trying to add a VC to your navigation stack ? – Teja Nandamuri Mar 03 '16 at 15:14
  • 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 Mar 03 '16 at 15:18
  • 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 Mar 03 '16 at 16:45

1 Answers1

1

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:

Workshed
  • 236
  • 1
  • 8
  • That maybe a solution. Does the NavigationController need a rootViewController? – Vancore Mar 03 '16 at 17:37
  • 1
    Yes, if you create a navigation controller in a storyboard you'll see it creates a root view controller linked to it by default. – Workshed Mar 03 '16 at 19:41
  • Thank you very much, that solved my problem :). Do you know how I can access the navigationbar at the top? I want to add a button that pushes to another viewcontroller, how is that achievable? – Vancore Mar 04 '16 at 07:58
  • 1
    Have a look at this to get you started http://stackoverflow.com/questions/5725049/adding-a-right-done-button-uibarbuttonitem-to-a-uinavigationcontroller – Workshed Mar 04 '16 at 08:19
  • I read the docs ... that was enough to solve this problem as well. Maybe I am crying for help too early sometimes! :D ... thank you again! – Vancore Mar 04 '16 at 08:25