Glenn Glenn - 1 month ago 9
Objective-C Question

UINavigationController does not call viewDidLoad on pushed UIViewController

I'm facing an odd problem concerning pushing a UIViewController into a UINavigationController.

CustomTableViewController
is a subclass of
UITableViewController


CustomTableViewController* vc = [[CustomTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc];
[self.view addSubview:navVC.view];


For some reason, the Navigation Controller is loaded and visible on screen but
viewDidLoad
is never called on
CustomTableViewController
.

Manually pushing the VC afterwards also does not work (still not loading the
vc
's view)


  • [navVC pushViewController:vc animated:YES];



However, the following methods DO call
viewDidLoad
on
vc
:


  1. [self.view addSubview:vc.view];

  2. [self presentViewController:navVC animated:YES completion:nil];

  3. [self presentViewController:vc animated:YES completion:nil];



So I tend to believe that there is nothing wrong with the View Controller nor the Navigation Controller. However, I have no clue why the view is not being loaded. Manually forcing [vc view] will load the view but then the other view lifecycle methods are not called.

There is also nothing wrong with
CustomTableViewController
, as using
UITableViewController
itself also doesn't work.

Any ideas?

Answer

You need to add it as child view controller

    [childController willMoveToParentViewController:rootViewController];
    [rootViewController addChildViewController:childController];
    [rootViewController.view addSubview:childController.view];
    [childController didMoveToParentViewController:rootViewController];

Hope this works

Comments