Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't viewWillDisappear or viewDidAppear being called?

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?

like image 895
jamone Avatar asked Nov 27 '10 01:11

jamone


People also ask

Why does viewWillAppear not get called when an app comes back from the background?

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 ...

Which is called first viewDidLoad or viewDidAppear?

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.

How many times is viewDidAppear 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.

What is called after viewDidAppear?

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.


3 Answers

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.

like image 120
Alex Wayne Avatar answered Nov 07 '22 18:11

Alex Wayne


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
    } 
}
like image 36
Tarun Mittal Avatar answered Nov 07 '22 19:11

Tarun Mittal


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
like image 25
Kendall Helmstetter Gelner Avatar answered Nov 07 '22 19:11

Kendall Helmstetter Gelner