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 knowi开发者_开发技巧ng something so simple at this point.
Thanks!
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:
.
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
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;
}
精彩评论