I am trying to get my head around child view controllers. I make a simple project with a main view controller.
Step 1: In that view controller I put a UIButton that is supposed to make a child view appear.
Step 2: I add a container view and put a red background for visibility and a button that will later dismiss it.
I run the project to see what it looks like so far.
Now, the first problem that I encounter is that I don't want the child view to be visible from the begining, I want it to appear when I hit the green button. So I know I'll have to hide it somehow. The two options I have looked at so far are
self.vc.view.hidden = YES;
self.vc.view.alpha = 0;
But the question that I have is that I don't fully understand how to access this child view controller to do either. Am I right to think something along these lines:
h file for the parent view controller:
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ViewController : UIViewController
@property ChildViewController *vc;
- (IBAction)CreateChildVC:(id)sender;
@end
m file:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.vc = [[ChildViewController alloc]init];
[self addChildViewController:self.vc];
[self.view addSubview:self.vc.view];
self.vc.view.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)GoToView2:(id)sender {
}
@end
This is not working as when I run it the child view is visible. I have tried changing something drastic like the color background to black via:
self.vc.view.backgroundColor = [UIColor blackColor];
and it's not affecting the child view controller in any way. Do I have to use something like
self.vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildVC"];
There are so many options it's easy to get lost. Any help much appreciated.