views:

101

answers:

3

Hello.

I'm very new on iPhone development. I wondering where to put some custom initialization of an instance variables for my UIViewController.

Can I use initWithNibName:bundle:?

Thanks.

A: 
- (void) viewDidLoad {
    [super viewDidLoad];
    //instantiate here...


}

This method is called when the view is ready. You can use that.

//Edit..

Hmm did you mean if you instantiate a UIViewController with a property called, e.g. "myVariable" and you want the "myVariable" to be, say, myVarible = @"Hello"; then you can do it on the instance, if it is a synthesized property.

MyVC *viewController = [[MyVC alloc] initWithNibName:@"MyNibForMyVC" bundle:[NSBundle mainbundle]];
[viewController setMyVariable:@"Hello"];
RickiG
A: 

It depends on your instance variable I suppose.

If the instance variable is visible from clients of MyViewController, then use RickiG's suggestion.

If it is a helper instance variable basically being used only by MyViewController, then just make your own version of initWithNibName:bundle that calls the super's version, and initialize your instance variables there...

Lyndsey Ferguson
+1  A: 

initWithNibName:bundle: is the designated initializer, and should be used for custom initialization of the view controller. You should use this for the instance variable code that should run once, on creation of the view controller, except for views controlled by this view controller.

The views may come and go in the run of the application. Therefore you may want to initialize the view more than once from one view controller object. You should place the view initialization code in loadView if you are not using a nib for the view, or viewDidLoad if you are using a nib.

Mr. Berna