PDA

View Full Version : How do I do something when a view first loads?




danmwall
Apr 17, 2009, 09:46 AM
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??



BlackWolf
Apr 17, 2009, 09:50 AM
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.

Niiro13
Apr 17, 2009, 10:00 AM
You could also try the viewWillAppear method.

BlackWolf
Apr 17, 2009, 10:05 AM
You could also try the viewWillAppear method.

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 ;-)

dejo
Apr 17, 2009, 10:57 AM
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.)

danmwall
Apr 17, 2009, 12:04 PM
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!!!

BlackWolf
Apr 17, 2009, 12:21 PM
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!!!

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