views:

1090

answers:

1

Hi, I'm learning to develop for iPhone without Interface Builder (a personal preference), and I'm not sure which function of a view controller I should be setting up the view in: init, viewDidLoad, or loadView. I read somewhere to only use init for the view controller, if anything, and not setting up the view as it could cause problems. I also read that viewDidLoad is only for loading from nib files, but It worked when I tried using it.. so what stuff should I put in each of these 3 methods?

Thanks!!

+2  A: 

Well each method has its own purpose

1) init

This method is intended to just initialize the ViewController , you are not required to override this method, but if you want to do some custom initialization BEFORE any views are loaded then this could be a place to do it. You have different flavors of the init methods, you can look at the in the docs at apple site.

2) loadView this method here is used to programatically create your view. If this method is not overriden, the default w ill create an empty view for you, but if it is you MUSt initialize viewController.view property, this gets called when a UIViewController view gets pushed into a super view.

3) viewDidLoad this method is called after you view has loaded on the screen (After loadView has been called and the view is pushed on the super view or window). Here you can do you can add subViews to your controllers views and also do other set up that you want to occur once your view loads. This method works regardless of making a view f rom a nib or programatically.

Daniel
Are you sure because this is from the apple docs on viewDidLoad:If a view controller is unarchived from a nib file, this method is invoked after its view is set. Therefore, subclasses should override this method, not the loadView method, to initialize objects loaded from a nib.----------- That pretty much says nib -> viewDidLoad, programatically -> loadView, right?
Mk12
no, this is saying if you using a nib you DONT override loadView because the view setup is already done for you in the nib. thats all this is saying
Daniel
so if you want to do additional setup in a viewcontroller where you are using a nib as a view, you have to do it in viewDidLoad, cant do it in loadView
Daniel
Ok so loadView -> Only if I need something other than a plain UIView, like if I had my own UIView subclass, usually don't even override viewDidLoad -> set up all the controls, and add them to the self.view. and init -> only if there's something about the view controller I need to initialize.
Mk12
Something like that, you can pick and choos if u want to do ur view components setup in viewdidload or loadview when not using a nib
Daniel
Oh wait I actually misunderstood what you meant when you said MUST because I thought you meant you shouldn't override, but you actually meant you have to set the view property. So now I think what I'll do is: [init]: Any stuff I need to do with the view controller before any views are loaded [loadView]: Create the view and set the self.view property [viewDidLoad]:Create all the controls, set the propertie with them (if they have a property) and add them to the subview. Thanks!
Mk12