Search code examples
objective-cuicontainerviewchildviewcontroller

Container Views and presenting child view controllers


A question about container views in xCode with objective-C. When I add a container view to my project it is visible by default in it's parent view controller. My goal is for it to be invisible to start with and to present it via a UIButton with the

addChildViewController:

method. But for that I need it to not be there to begin with. I can fix this by setting it's view.alpha to 0, but it seems long winded, to have something that's visible and have to make it invisible to then make it visible again. It doesn't seem best practice. I've checked the doccumentation and found a lot of interesting things but nothing on this particular subject. Can anyone point me in the right direction? Thanks

---------------------------UPDATE----------------------------------------

This is what I've done so far. (there's a @property ChildViewController *vc in the h file with the required import). It's not working. As in, it's not hidding anything.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.vc = [[ChildViewController alloc]init];
    [self addChildViewController:self.vc];
    [self.view addSubview:self.vc.view];
    self.vc.view.hidden = YES;
}

Solution

  • Hiding the View Controller by setting its alpha is totally fine. It has the advantage that your view controller is ready to go so the app will feel more responsive. The alternative is to use 'presentViewController' to actually add it (modally) to the view. Thats probably the more normal way but really either is fine. The only time it wouldnt be a good idea is if that view controller that you are show/hiding is very processor or memory intensive, and you need your app to be more efficient with resources, in which case you should present and dismiss it modally rather than show/hiding it with alpha.