I have 1 view controller acting as a CustomViewContainer and it has 2 child view controllers with only 1 added initially at launch.
Invoking addChildViewController from the Container on the child and then a subsequent invoke of didMoveToParentViewController on the child doesn’t display my child view on the screen. Instead, I still see the parent controller’s view.
@implementation ContainerViewController // inherits from UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
LOG_METHOD_SIG();
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
// ChildViewController is a subclass of UIViewController
_childController = [[ChildViewController alloc] initWithNibName:nil bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:_childController];
// Expected the following line to display the child controller's view
[_childController didMoveToParentViewController:self];
}
If I add [self.view addSubview:_childController.view] after 'addChildViewController', the view is shown. But I thought that the whole point of container view controllers was to avoid direct view manipulation.
In the case where you’re adding your first child view controller, could you call ‘transitionFromViewController` and if so, what would be the ‘from’ view controller?
addChildViewControllerdoes just that: it adds a child view controller. It does nothing with its view.transitionFromViewController:toViewController:duration:options:animation:completion(or whatever…) is where the magic happens. Your fromViewController could be the view controller you added initially, the toViewController is your second view controller.