0

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.

enter image description here

Step 2: I add a container view and put a red background for visibility and a button that will later dismiss it.

enter image description here

I run the project to see what it looks like so far.

enter image description here

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.

Paul
  • 1,045
  • 4
  • 22
  • 46
  • Why would you need an additional controller for your subview? – Tim Vermeulen Feb 29 '16 at 16:37
  • I want to add a uitable and other controls to it. Is that not the most logical way to do it? – Paul Feb 29 '16 at 16:58
  • Usually you would just add the table and the other controls as outlets to the current controller. – Tim Vermeulen Feb 29 '16 at 17:13
  • Hi @Paul, what you are doing is exactly correct. – Fattie Jan 27 '17 at 13:36
  • I can't totally follow your question 100% but I think you just want this: http://stackoverflow.com/a/23403979/294884, go to the section "How to connect 'to' the child controller..." – Fattie Jan 27 '17 at 13:40
  • Possible duplicate of [How to add a subview that has its own UIViewController in Objective-C?](http://stackoverflow.com/questions/23399061/how-to-add-a-subview-that-has-its-own-uiviewcontroller-in-objective-c) – Fattie Jan 27 '17 at 13:40

0 Answers0