R – iPhone Development – Setting up a view in a view controller

initializationiphonemethodsuiviewuiviewcontroller

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!!

Best Answer

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.