What I'm trying to achieve is to design my "shared" part of UI in interface builder and use child view controllers to show content to the user. This may sound as trying to reinvent UINavigationController
, but it is not. In fact, the whole thing is embedded in one.
Now, what I'm trying to do is change child view controllers of this BaseViewController
and indicate this change in the navigation bar, so that all of its functionality remains.
I tried adding such a method :
+ (UIViewController *)baseViewControllerWithChild:(UIViewController *)child {
BaseViewController *base = [BaseViewController new];
[base addChildViewController:child];
child.view.frame = base.childViewControllerContainer.frame;
[base.view addSubview:child.view];
[child didMoveToParentViewController:base];
return base;
}
and then using it like this :
- (void)didTouch:(UIButton *)sender {
[self.parentViewController.navigationController pushViewController:[BaseViewController baseViewControllerWithChild:[DummyViewController new]] animated:YES];
}
(Note : DummyViewController
is exactly that - a dummy vc, made just for testing, it only has background color set in viewDidLoad
)
This method is a handler of a button in first child view controller. So far so good. Unfortunately, the result is not as expected - the pushed view controller is black. At firs I thought this was because BaseViewController
was designed in storyboard
and initially set as rootViewController
of navigation controller. Moving it to a xib
file and setting from code didn't quite work for me, as you cannot add a Container View
in a xib
.
To summarise, I would like to have a base design governed by BaseViewController
class and content would be added as a childViewController
of it. Pushing a new view controller would be a result of an action on these childViewController
and should update the navigation stack accordingly.
Also, the whole thing needs to work with iOS 7.
Any help as to how to try to achieve this is greatly appreciated!