More than 1 appDelegate object?

Discussion in 'iOS Programming' started by HARDWARRIOR, Aug 10, 2009.

  1. HARDWARRIOR macrumors member

    Joined:
    Nov 17, 2008
    #1
    Hi
    While fixing third-party code I've discovered a really brilliant idea) Guy was using 2 appDelegate objects in project xibs. I assume he thought that this would be some kind of singletone or such. But after some rethinking of that piece of code, I found that there is no technical restrictions on it.
    Here is my example: simple project with navController and 2 views. Each with it's viewController. When app launched, first view is on screen. When user taps button, second view is pushed to navController. For now there is appDelegate object in MainWindow.xib. Now, if you'll add just the same appDelegate object to second view's xib. Now right when second view is pushed, you can see that one more instance of appDelegate is created and destroyed (if you'll override init and dealloc methods and insert log there).
    Here I'm very surprised. Does it mean that only one appDelegte instance can be created? If yes, then why? appDelegate is just a NSObject subclass implementing UIApplicationDelegate protocol.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Normally you only have one appdelegate object. It's used as a singleton. Since UIApplication is a singleton its delegate is also a singleton. In theory you could set UIApplication's delegate property to different objects at different times but I've never come across a reason to do that.

    I did come across an apple sample code example that had a bug in it that was caused by having more than one app delegate. Can't remember which one it was.
     

Share This Page