I just stumbled on a small problem that gave me a great headache. I changed the view hierarchy to implement flipping preferences on the back of my App’s main view. Accordingly, I had to move some stuff out of the nibs and into the code. Iin other words, some heavy refactoring, and without testing (I know, bad bad me).
So basically I now have a root UIViewController
which has a UINavigationController
as a subview; this, in turn, loads a number of UIViewController
s to navigate my items hierarchy.
Today, while working on cosmestic/usability improvements, I realised that something was definitely not working. When moving back and forth in my items, data were not refreshed. This pointed to one culprit, viewWillAppear not being called in the UIViewControllers.
After some googling I found out that if you add a UINavigationController
as a subview of a UIViewController subclass, you must explicitly call its viewWillAppear method from its container; otherwise, they won’t be called, and when moving back and forth in the navigation tree, your UIViewController
s’ viewWillAppear:
methods won’t be called.
It’s quite simple; assuming that projectNavigationController
is a navigation controller added as a a subview of this UIViewController
subclass, just make sure you add this simple call:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[projectNavigationController viewWillAppear:animated];
}
Of course the same applies to your other viewWill/Did methods
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[projectNavigationController viewWillDisappear:animated];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[projectNavigationController viewDidAppear:animated];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[projectNavigationController viewDidDisappear:animated];
}