applicationWillTerminate not firing...

Discussion in 'iOS Programming' started by MrCrispy, May 3, 2010.

  1. MrCrispy macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #1
    I've got a utility app. On the flipside view, there is an applicationWillTerminate that fires when it should (home button, phone call, etc) but on the main view, I've also got an applicationWillTerminate that doesn't fire no matter what I do.

    Any thoughts as to why this would be happening?
     
  2. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #2
    Where do you have the applicationWillTerminate command? It should only be in the app delegate.
     
  3. MrCrispy thread starter macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #3
    I've got one in the MainView and the Flipsideview. The reason is that if the application terminates in the flipside it needs to do something entirely different than if it's got the main view showing.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    applicationWillTerminate is a method in the application delegate protocol. So it will only be called for your app delegate. It is possible to register other methods in your app to be called when your app is terminating, but you must explicitly register them.

    Code:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
    If you do this in a view controller be sure to unregister when the view controller is dealloced.

    I believe that the view controller method viewWillDisappear: is also called when the app quits.
     
  5. MrCrispy thread starter macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #5
    I'll give the viewWillDisappear thing a try as I'm still trying to (unsuccessfully) wrap my head around the whole delegate thing in the first place. For some reason it's just not making my two brain cells bang together just right to grasp the concept.

    Barring the willDisappear thing, is there a way in the delegate to check what view was currently showing?

    Kind of a:
    Code:
      if (mainViewIsActive) {
         //Do the stuff that needs to be done on the main view when it closes
     } else {
        //Do the stuff specific to the FlipSideView
     }
    
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    is there a way in the delegate to check what view was currently showing?

    Not sure, depends on the structure of the app. The example code for the Utility app has changed since I looked at it. Most likely you'll have to set a flag yourself to check which view is showing.

    The idea though of registering for the applicationWillTerminate notification is that the view controllers would register for that callback when they go active and unregister when they go inactive. That way the view controller will do the stuff it needs to do when the app terminates.
     

Share This Page