first thing first, which method to help

Discussion in 'Mac Programming' started by Howiieque, Aug 9, 2009.

  1. Howiieque macrumors regular

    Feb 1, 2009
    - (void)awakeFromNib
    - (void)applicationDidFinishLaunching:(UIApplication *)application
    - (void)viewDidLoad

    it seems that these three methods share some things in common. they all let me do some initialisation tasks. could someone explain the differences amongst them to me? for instance, if i compute 1 plus 1 and then put the result in the instance variable, which one should be the best.
  2. JoshDC macrumors regular

    Apr 8, 2009
    It really depends on the scope of the initialization. If it's a something that only affects a given view, use viewDidLoad:; if it affects or links a number objects in a nib, use awakeFromNib:; if it affects the entire application (like a global variable) use applicationDidFinishLaunching:. There are a number of cases where either will give the desired affect, and it's more an issue of where makes the most sense.
  3. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thank you very much.

    do they happen in a certain order?
    i think it might be:

    - (void)viewDidLoad
    - (void)awakeFromNib
    - (void)applicationDidFinishLaunchingUIApplication *)application

    do you agree?
  4. admanimal macrumors 68040

    Apr 22, 2005
    First of all, you seem to be assuming that you will only have one view/nib in the whole application, which of course is not always true. However, even if that is the case, applicationDidFinishLaunching will almost definitely be called before viewDidLoad and probably before awakeFromNib, although I have never used the latter method so I don't know for sure. I'm also guessing that awakeFromNib will occur before viewDidLoad.

    You can easily figure out which method is called first in your own particular app by setting breakpoints in each method in XCode and seeing what order they are hit in.
  5. Howiieque thread starter macrumors regular

    Feb 1, 2009
    admanimal, thank you for pointing out my serious mistake and the break point suggestion.
    here is the result( based on single view and single view controller app, but just one certain app, so maybe on other tests the results will vary):

    1st- (void)awakeFromNib----(the one in xxxAppDelegate.m get called first, then the one in xxxViewController.m)---xxx represent the name of my app
    2nd- (void)applicationDidFinishLaunchingUIApplication *)application
    3rd- (void)viewDidLoad

    - (void)viewDidLoad
    This method is called after the view controller has loaded its associated views into memory--apple doc

    the associated views i think is the the views that are pointed via controller's outlet. am i right this time? that is the UIView. excluding the buttons, textfield, etc. on it.
  6. JoshDC macrumors regular

    Apr 8, 2009
    Yes, it's the view controller's view outlet, but all the buttons and subviews should also have been loaded, enabling you to programatically set titles, actions, etc.
  7. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thank you, JoshDC.
    but i still have something can not understand.

    i have a tableview on the view. i prepared an array inside the - (void)viewDidLoad method for display in the tableview.
    the point is if the when the view is loaded means the tableview is loaded. but how can the table fetch the data from the array.

    or maybe i confuse the concept of loaddidload and the tableview fetch data and have them displayed. when does tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath get called?

    please tell me a bit more.:D
  8. Cinder6 macrumors 6502


    Jul 9, 2009
    Pretty sure it's awakeFromNib, applicationDidFinishLaunching:, and viewDidLoad. I had a problem with a variable being nil yesterday in awakeFromNib; changing to applicationDidFinishLaunching: fixed it.

    As for the (NS|UI)TableView--you have to set the data source methods (which it sounds like you know). You want to have your array stored outside of the viewDidLoad method (probably an instance variable of your class). The TableView will then query your class for the number of rows, and the object (or cell, in the iPhone's case) that goes in each row/section.

    tableView:cellForRowAtIndexPath: gets called whenever the TableView needs data. This means upon startup and whenever you manually tell it to reload. I'm not 100% sure on when it first does this, but it happens after applicationDidFinishLaunching:, for sure. When in doubt, you can always tell it to reloadData.
  9. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thanks, Cinder6.

    now the answer has become more and more clear after a step by step debug.

    - (void)applicationDidFinishLaunching:(UIApplication *)application
    was first invoked.

    by default. there are two statements inside it:

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    right before [window addSubview:viewController.view] finish executing, the - (void)viewDidLoad was called.
    and after - (void)viewDidLoad finishing, [window addSubview:viewController.view] comes to its turn again.

    so, sure enough, - (void)viewDidLoad finishes before - (void)applicationDidFinishLaunching:(UIApplication *)application completely executes.

    tableview being loaded and it fetching cells on it seem to be two different concept. not until the - (void)applicationDidFinishLaunching:(UIApplication *)application finished, have the tableview fetched its cells.

Share This Page