开发者

UIViewController: where can I put custom initialization

I'm very new on iPhone development. I wondering where to put some custom initialization of an instance variables for my U开发者_JS百科IViewController.

Can I use initWithNibName:bundle:?

Thanks.


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.


- (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 it 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"];


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 super's version, and initialize your instance variables there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜