How do I do something when a view first loads?

Discussion in 'iOS Programming' started by danmwall, Apr 17, 2009.

  1. danmwall macrumors newbie

    Apr 13, 2009
    I am a total noob, and for some reason I just can't figure this out. When my program launches or when I switch to a different view (different nib), it loads fine and I can see whatever I designed in the Interface Builder, but I want to perform some functions that impact how the view loads, and I can't figure out how to do that!

    For example, I am designing a trivia game. From the main menu of the app, the user selects the option to start playing the game. I load the new screen, but I can't seem to populate the buttons and the labels when the screen first loads! I have to push a button on the screen in order to populate the screen with data and animate the buttons. There has to be a way to eliminate the need to push a button first, but I'm too inexperienced to know what that is.

    It seems like I should be able to accomplish that in the viewDidLoad method, but that's not doing the trick.

    Can someone help me?? Please??
  2. BlackWolf macrumors regular

    Apr 9, 2009
    if viewDidLoad is not doing the trick, you're definetly doing something wrong, because that method is invoked when your view loads (who would have thought?).

    do you get any warnings or errors? did you try putting an nslog in your viewDidLoad method like this:
    NSLog(@"here we go");
    to see if the method is invoked at all?

    how do you load your view? are you using a viewcontroller? because viewDidLoad is the method of a UIViewController, not the UIView itself!

    maybe you want to post some code you think is relevant.
  3. Niiro13 macrumors 68000

    Feb 12, 2008
    You could also try the viewWillAppear method.
  4. BlackWolf macrumors regular

    Apr 9, 2009
    afaik, viewWillAppear is invoked before the view appears (of course) so you can't be sure all the UILabels and whatnot are already loaded. not 100% sure about this though.

    anyway, I go with viewDidAppear to be sure :D

    edit: well, no, I use viewDidLoad, but I WOULD use viewDidAppear to be sure ;-)
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    There is both viewWillAppear and viewDidAppear. And you will be fine knowing you have access to labels, etc. in the viewWillAppear since those are set up during the init or the viewDidLoad. (Unless of course you have code where you release these objects before viewWillAppear is called.)
  6. danmwall thread starter macrumors newbie

    Apr 13, 2009
    I was able to execute the code in viewDidLoad, viewDidAppear, and viewWillAppear, but it just wasn't really doing anything to my screens. Because of your suggestions, however, I stumbled across the awakeFromNib function which totally works!!

    Thank you, thank you, THANK YOU!!!
  7. BlackWolf macrumors regular

    Apr 9, 2009
    still, the other methods should work as well, so I guess there is still something wrong in your code!

Share This Page