Arthur Skirvin
Arthur Skirvin

Reputation: 1210

iPhone: Executing code Just Once at Initiation of Object

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!

Upvotes: 0

Views: 287

Answers (3)

Tom Harrington
Tom Harrington

Reputation: 70946

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:.

Upvotes: 1

David Sowsy
David Sowsy

Reputation: 1680

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

Upvotes: 0

mythz
mythz

Reputation: 143319

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

Upvotes: 1

Related Questions