2

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.

It looks like this : example view

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!

Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • You say, "BaseViewController was designed in storyboard". Should you be using the storyboard to instantiate it instead of just creating a standalone object? – Phillip Mills Dec 24 '15 at 13:18
  • I also tried having it designed in a `xib` as explained, but it didn't help me though. Also, it was not instantiated via `[UIStoryboard instatiateViewController...]` but view `new` (or `alloc init`) as it is the same thing. I'm fine with designing the views in code if need be, but wanted to use interface builder as much as I could. – Losiowaty Dec 24 '15 at 13:29
  • Try using `instantiateViewControllerWithIdentifier:`. It's really not the "same thing". – Phillip Mills Dec 24 '15 at 13:31
  • Yukes, "same thing" was meant to relate to `new` and `alloc init` - Ican see the confusion. I've changed my method to instatiate `BaseViewController` via storyboard - it didn't help, unfortuantelly. I've managed to pinpoint the issue though - `childVieControllerContainer` is an `IBOutlet` and it is `nil` in my static method (which is obvious when using `new`, but I thought that using `instantiateViewController...` would set them up correctly. – Losiowaty Dec 24 '15 at 22:13

1 Answers1

0

The issue was casued by base.childViewControllerContainer being nil - this was caused byt he fact that view property of view controllers is loaded lazily. Adding [base view] before accesing base.childViewControllerContainer solved the issue, though I'm not sure if this is the one, only and best way to do this.

Losiowaty
  • 7,911
  • 2
  • 32
  • 47