I have a UINavigationController
with a UITableView
as my main menu. User clicks on a cell and a new view is pushed on the stack. In one case I push another UITableView
that needs a toolbar. So on that 2nd tableView's init I setup the self.toolbarItems
property with the correct items. But then I need to call [self.navigationController setToolbarHidden:NO animated:YES];
So it makes sense to call this in the viewDidAppear
or viewWillAppear
method. But I put it in those methods and find out (Also via NSLog) that they never get called. The same goes for hiding it in viewWillDisappear
or viewDidDisappear
. Why don't these methods get called? Where should I be doing this hiding/showing of the toolbar then?
In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible ...
viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.
maybe you invoke viewDidAppear in viewDidLoad (or some other stuff is going on there), since it's invoked only once during loading the view from the memory. It would match, that it's invoked two times only the first time.
viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded. if you push and then pop any other viewController on that view then again viewDidAppear gets called.
I have noticed behavior where if a parent controller (like UINavigationController
or UITabBarController
) never get's viewWill/DidAppear
called on it, it won't call it on the child controllers either. So make sure that in the code where you create the parent controller, you call viewWillAppear
, show it, then call viewDidAppear
. Then it should make those calls on it's child controllers as is appropriate.
Double check the parent controller is having those methods called, and call them yourself if they are not.
Yes Its true
you can do this by first write this code in
- (void)viewDidLoad {
self.navigationController.delegate = self;
}
And then write the code which you want to write in viewWillAppear
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[self class]]) {
//write your code here
}
}
Although you solved your problem, in case someone comes along in the future another problem could have been that you forgot the animated: argument to either method - that is to say, the format of the method needs to look like:
- (void) viewWillAppear:(BOOL)animated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With