Reputation: 1625
I am designing a custom UIViewContainerController
for a project. I keep a contentView
in this container to manage my Childviewcontroller's
views. When I add a childviewcontroller
alone it works fine. But some of the childviewcontrollers
must be initialized with a "navigationcontroller" & that is where I'm having problems with implementation.
I use the usual initWithRootViewController
method on "navigationcontroller" to init(initialize) my "childvc" & then how do I add this along with the navigation bar to my contentView
?
This is the code I'm using for "childvc" without a "navigationcontroller" & it works fine.
// in my containerview controller's add childview method.
ChildViewController1 *vc = [ChildViewController1 new];
[self addChildViewController:vc]; // self = container vc
vc.view.frame = self.contentView.bounds;
[self.contentView addSubview:vc.view]; // contentView is the space i've kept to add childvcs
[vc didMoveToParentViewController:self];
Now when I try to use a "childvc" initialized with "navigationcontroller" (because there is a flow to this "childvc") I get errors & what I need to know is how do I add this to my contentView
along with navigation bar. (just like in a tabbarcontroller).
This is the code I use to initialize "childvc" with "nav controller" :
ChildViewController1 *vc = [ChildViewController1 new];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
I've made public repository with simple project "Here".
I have read doc of tab bar / nav controllers & creating custom container viewcontrollers in apple documentation but seems to be missing something critical. Link is "Here".
Upvotes: 1
Views: 1488
Reputation: 13388
From looking at the commented code in your public repo, what you are trying to do is this:
container VC
+-- navigation VC
| +-- child VC
+-- child VC
This is wrong, the child VC can appear only once in the view controller hierarchy. Your hierarchy should look like this:
container VC
+-- navigation VC
+-- child VC
Here's a rough sketch of how the code for setting this up might look like. Notice how the navigation controller (and its view) completely replaces the ChildViewController1
.
// Setup the view controller hierarchy - place this inside
// your container VC's initializer
ChildViewController1* vc = [ChildViewController1 new];
// With this statement, vc becomes the child of the navigation controller
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.childViewControllers addObject:nav];
[nav didMoveToParentViewController:self];
// Setup the view hierarchy - place this inside your
// container VC's loadView override
nav.view.frame = self.contentView.bounds;
[self.contentView addSubview:nav.view];
As mentioned in the comments, I suggest that you separate the setup of the view controller hierarchy from the setup of the view hierarchy.
UINavigationController
does this with its initWithRootViewController:
initializer.loadView
override.Upvotes: 1