ViewDidLoad not called when adding to Container View

circuitlego picture circuitlego · Jul 30, 2013 · Viewed 7.7k times · Source

I have a container view set up in my storyboard file but I want to embed the view controller programatically after setting some properties. This is my code in the parent VC's viewDidLoad:

_workoutVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"entryTableVC"];
_workoutView = _workoutVC.tableView;

[self addChildViewController:_workoutVC];

[_workoutVC.tableView setFrame:_container.bounds];
[_container addSubview:_workoutView];

However, viewDidLoad in the child is not called at any time. When I run the simulator my container view is blank. I've checked in the debugger that none of my properties are nil.

Answer

Adam picture Adam · Jul 30, 2013

The viewDidLoad: method is called when the view property is accessed for the first time. I assume that in your case view == tableView. If not, you have to adjust the loadView method of your child VC.

- (void)loadView {
    //create your table view here
    //...
    self.view = self.tableView;
}

Then just use view property instead of tableView.

_workoutView = _workoutVC.view;

EDIT:

The full code would be:

_workoutVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"entryTableVC"];
_workoutView = _workoutVC.view;

[self addChildViewController:_workoutVC];

[_workoutView setFrame:_container.bounds];
[_container addSubview:_workoutView];