where should i place [super viewDidLoad]

Discussion in 'iOS Programming' started by Howiieque, Aug 8, 2009.

  1. Howiieque macrumors regular

    Joined:
    Feb 1, 2009
    #1
    hi.
    this code is from beginning iphone development
    - (void)viewDidLoad {
    ......
    UIApplication *app=[UIApplication sharedApplication];
    ......

    [super viewDidLoad];
    }

    my question is that why did not the author make [super viewDidLoad] the first statement. according to some rules, we should set up the things from its superclass and then itself. the author did so in both versions of his books. is there a special purpose?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Unfortunately there's not a good answer to this question. In the apple docs in some cases it's made clear that the base class implementation of a method does something important and in other cases it's made clear that the base class method does nothing important. In most cases it isn't mentioned. However, in the apple iPhone forums some of the apple engineers sometimes emphasize that user code should call the base class implementation of this method and others like it but no clear reason is offered.

    FWIW, I've never called super viewDidLoad in my code and I'm, not aware of any problems because of this. My assumption is that UIViewController viewDidLoad doesn't do anything. I might be wrong about that and they might change things in the future. Such is life on this platform.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Probably not. I would bet the supers aren't doing anything special (if at all) in their viewDidLoads and so it doesn't make any difference. But, as you mention, it's normally good practice to let the super handle its duties first before you add yours.

    You might try asking the authors of the book, at their website, if this was just by accident or there was a reason.
     
  4. Howiieque thread starter macrumors regular

    Joined:
    Feb 1, 2009
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Also, just wanted to add a comment about a similar (and new) method: viewDidUnload. Inside that you probably want to be putting your code before the [super viewDidUnload];
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    viewDidUnload is exactly the kind of method that I was talking about. The docs mention nothing about needing to call super from that method and its purpose makes me think that there's no need to call super. FWIW, I don't call super from my code and I don't see any problems.

    If you had a hierarchy of view controllers, that is you have a view controller that inherits from another one of your own view controllers, then it would make sense for the derived class to call super. Otherwise I don't see any reason to do that.

    Just to add, if I look through Apple's sample code it's a mixed bag. Some of it calls super from viewDidLoad/viewDidUnload and some of it doesn't.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Actually, Chapter 9: Navigation Controllers and Table Views from the "Beginning iPhone Development" book does just that. You create a SecondLevelViewController class that's like an abstract class and then subclass that class for each of the six second level views.

    And if calling the super doesn't hurt most of the time, I don't see any reason not to just include it, just in case. Unless you have specific reason that calling it is causing an issue.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    My logic is the opposite. Why add it if it adds no known benefit and the docs don't require it? It's just unneeded overhead.

    IMO, this is some kind of laziness on Apple's part, or maybe just an internal disagreement. Adding code because of 'just in case' doesn't make any sense to me.
     
  9. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    If the super is initializing something you need to use or are modifying (alloc-ing stuff you are about to write to), put it first, or you will trash memory or crash.

    If the super is cleaning up (releasing stuff are using), put it at the end, or you will trash memory, over-release or leak stuff.

    If none of the above, why bother?

    If you don't know, you could have a major problem.
     
  10. Howiieque thread starter macrumors regular

    Joined:
    Feb 1, 2009
    #10
    thank you firewood, your lines really makes sense.
    i don't know what his super class will do. its super class is UIViewController.
    but i think it might fall into the first case, and [super viewDidLoad is going to the first statement.
    thanks all of you.
     

Share This Page