I’m trying to create a container view controller to hold a UINavigationController and a custom UIViewController together on screen.
I laid it out in a test program and it works perfectly, however when I tried to implement it in my real project the appearance methods for the containing View Controllers never get called.
Working Test:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterStatusViewController *master = [[MasterStatusViewController alloc] init];
[self.window setRootViewController:master];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
InventoryViewController *newInventory = [[InventoryViewController alloc] init];
self.navigation = [[UINavigationController alloc] initWithRootViewController:newInventory];
self.statusRibbon = [[StatusBarViewController alloc] initWithNibName:@"StatusBarViewController" bundle:nil];
[self addChildViewController:self.navigation];
self.navigation.view.frame = self.navigationView.frame;
[self.view addSubview:self.navigation.view];
[self.navigation didMoveToParentViewController:self];
[self addChildViewController:self.statusRibbon];
self.statusRibbon.view.frame = self.ribbonView.frame;
[self.view addSubview:self.statusRibbon.view];
[self.statusRibbon didMoveToParentViewController:self];
}
Failing Project:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MasterViewController *master = [[MasterViewController alloc] init];
self.centerController = master;
[self.window setRootViewController:master];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
LoginViewController *login = [[LoginViewController alloc] init];
self.navigation = [[UINavigationController alloc] initWithRootViewController:login];
self.statusRibbon = [[StatusRibbonViewController alloc] init];
[self addChildViewController:self.navigation];
self.navigation.view.frame = self.navigationView.frame;
[self.view addSubview:self.navigation.view];
[self.navigation didMoveToParentViewController:self];
[self addChildViewController:self.statusRibbon];
self.statusRibbon.view.frame = self.ribbonView.frame;
[self.view addSubview:self.statusRibbon.view];
[self.statusRibbon didMoveToParentViewController:self];
}
The views are arranged correctly and perform as expected, except the appearance methods never get called. I’m quite perplexed. Calling the methods manually doesn’t seem to be a solution, as those calls aren’t going through either.
EDIT: By appearance method I mean viewWillAppear, viewDidAppear, et al.
SECOND EDIT:
~ snip ~
THIRD EDIT: Upon further inspection, I believe the reason the appearance lifecycle methods aren’t being called is because they aren’t being called on the container view controller either. I added
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"MasterViewController VIEW WILL APPEAR");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"MasterViewController VIEW DID APPEAR");
}
to the masterViewController and nothing, for some reason they are never being called.
Solved it. I don’t know why the master appearance methods were not being called, a few cleans of xcode seemed to solve that problem.
However, those methods still weren’t being forwarded to my other view controllers. Then I came across this link and learned that you always have to forward appearance calls to them no matter what. I was able to fix it by adding: