views:

60

answers:

3

Hey everyone, this is an EXTREMELY beginner question, and I'm somewhat ashamed I don't know it already: How can I execute code just once at the implementation of my object? I have an object that's of a subclass of UIView I want some code to be executed as soon as everything kicks off, but I'm only able to get code to be executed in response to user input. ARGHH!

I though -(void)viewDidLoad would work, but to no avail...

Any help is, of course, greatly appreciated. I hate not knowing something so simple at this point.

Thanks!

+1  A: 

The - (id)init and the - (id)initWithCoder:(NSCoder *)coder methods only gets called once when the view is created. Otherwise you can also use define symbols:

#ifndef HAS_INIT
#define HAS_INIT
  ...
#endif
mythz
The -(id)init function doesn't seem to execute either, and I get the warning "Control reaches end of non-void function." -(id)initWithCoder:(NSCoder *)coder gives me the same warning and just crashes when run. And when I use define symbols (exactly like above but [self viewDidLoad]; instead of ...) I get a Build fail with the message "Expected identifier or '(' before '[' token," and I don't know how to handle that error, being new. Also I'm convinced there should be a way to do this without using define symbols. grrrrrrr
Arthur Skirvin
A: 

For the rare occasion I need to do something like this, I create a variable in the header.

BOOL bRunOnce;

Then in the init,

bRunOnce = FALSE; 

Then in the function:

if (bRunOnce == FALSE){
  // do stuff
   bRunOnce = TRUE; 
}
David Sowsy
The issue I'm having is a little more basic than this. I can't get the object to execute a function/method at initiation/implementation/whatever the word for that is. I can only get it to run methods in response to user input.
Arthur Skirvin
Ah, check to see if the variables you are attempting to cause action to are nil at the time you think they should work. Chances are they have gone out of scope or have been released.
David Sowsy
+1  A: 

The viewDidLoad method is defined on UIViewController, not on UIView, so there's no reason to expect it to be called in a UIView subclass unless you call it yourself.

If you're creating the view in code, you'll want to look at -initWithFrame:. If you're using IB, use -initWithCoder:.

Tom Harrington
Thanks for answering, that's good to know. initWithCoder executes, but then it immediately crashes. Also the warning "Control reaches end of non-void function" is given within this method. I tried to return 0 and aDecoder, but it still crashes in both cases. Any ideas?
Arthur Skirvin
Ok, I figured out you return self. When I do this, however, my program doesn't work at all, only a white screen is displayed. Any ideas on this?
Arthur Skirvin
Did you call super's initWithCoder? You always need to call super's version in init methods (and really in most cases where you override a method).
Tom Harrington
Yep, that worked. I can't say I understand what's going on anymore, but it worked. MAAAAAANNNNNN I have a lot to learn. It's exasperating sometimes. Thanks a lot for taking the time to help out a newbie!!
Arthur Skirvin