viewDidAppear not firing ever again after app enters foreground

12,573

Solution 1

Found it.

While not new to programming I am new to iPhone development. On researching this problem I found it was not recommended to call the viewWillAppear and viewWillDisappear methods manually.

My viewWillDisappear methods resign any keyboards if shown, when my app enters the background it loads a splash screen ready for when the app re-enters the foreground (there is some logic I need to do to work out what the user is shown on restarting the app and I can do this under the splash screen).

As viewWillDisappear is not called when the app goes into the background to make sure no keyboards appeared over my splash screen I was calling viewWillDisapper in the applicationDidEnterBackground method. I guess this also un-registers my events.

By adding viewWillAppear to my applicationDidEnterForeground method my events started firing again. Lesson learned, I will refactor this so I don't call these events manually.

Thanks for the help.

Solution 2

Btw, the viewDidUnload method is really badly named btw -- it's not an 'opposite' to viewDidLoad, it's only called if there was a low memory situation and the view for that controller was unloaded due to not being visible at that time.

(ORIGINAL, NOT SO RELEVANT ANSWER:)

Please see my answer to this similar question:

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

Basically, viewDidAppear gets called after your UIViewController's view was added to the application's UIWindow heirarchy. Backgrounding then restoring the app doesn't change your view in that respect, so viewDidAppear doesn't get called -- it's correct behaviour, and not a bug. Check out the API docs for UIViewController.

Share:
12,573

Related videos on Youtube

Magpie
Author by

Magpie

Updated on June 04, 2022

Comments

  • Magpie
    Magpie almost 2 years

    I have traced a problem in my iPhone app code to the viewDidAppear method not always firing. When you start the app the event fires as expected. However if I close the app using a phone capable of multitasking and reopen in. My viewDidAppear events no longer fire.

    My views are loaded from Nibs and I use viewDidUnload to clean up (release and nil all outlets). My views are nested in side and tab bar then navigation controllers. I looks like the events aren't wired up properly when the nibs reload. Any idea on what I'm doing wrong/missing and how I can fix this?

    Thanks in advance.

    UPDATE I do not mean the event is not fired when the app first comes into the foreground. I mean the event never fires again. Even when changing between tabs or moving though the navigation views.

    Example:

    - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
       NSLog(@"viewDidAppear called");
    }
    

    This code is placed in two views, each on different tabs. Each time I swap between tabs "viewDidAppear called" is written to the log. When I close and reopen the app and swap between tabs this no longer happens. Other button events fire normally.

  • Magpie
    Magpie about 13 years
    :) it never gets called again. Even when I change between tabs, push and pop views. Once I've minimised and reopened the app this event is never fired again.
  • Kirby Todd
    Kirby Todd about 13 years
    make sure all your viewControllers are calling [super viewDidAppear] in their viewDidAppear method or the message can lost.
  • occulus
    occulus about 13 years
    Glad you sorted it. Your should accept your own answer - it's allowed and you're supposed to, if you find answer to your own issue.
  • Wirsing
    Wirsing over 10 years
    I strongly recommend this thread's solution over doing such stunts: stackoverflow.com/questions/15864364/…