Saturday, January 15, 2011

view and viewControllers

This post must have come earlier when I began writing blogs for iPhone as I get mails from people having one similar questions regarding the difference between a view and a viewController?. Well in this post I have tried to explain the difference.

You must have a TV set in your apartment where you can watch different programs like news, some reality shows, cartoons etc when you are traversing from one channel to the other channel via remote control the television set remains constant but the picture that the television set is showing changes as you traverse along the views. In a TV Program a character may appear for a moment and then be gone and then appear back.

The above statement is just a simpler version to make you understand the difference between view and viewController, the television set is just like the iPhone window and the TV programs are the view that you are seeing, the remote control is like the viewController. 

A viewController controls the views that you are using in your application, The UIViewController class will help your view to respond to some events like touches, device orientations etc with the help of some of its own methods. A view may appear for sometimes and then on some button touch you may see a new view on the iPhone window.

Each and every class like the UILabel,UIButton,UISwitch,UISlider etc are all inherited from the class UIView, If you are using the interface builder then you just drag a control and place it in the view window that is in front of you well, the view window is nothing but an instance of UIView class that you are seeing, in short you can place anything in the view window if you are using an interface builder, also if you are more the coding guy/girl who doesn’t love to drag drop things then you can have the instance of the UIView class in your application and then add views to the instance of the UIView class.

Some important instance method of the UIViewController class:

loadView: In this method you add the controls to the self view but before that you make a call to the super class loadview method so that the view that you are going to add in the self view are being added properly. This method is called when the view property of the viewController is accessed and you must use the loadView method only when you are adding views programmatically (without using Interface builder).

viewDidLoad: This method always gets called when we want do some manipulations with the views after they are loaded in the memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.

viewDidAppear: Notifies the view controller that its view was added to a window.

viewDidDisappear: Notifies the view controller that its view was dismissed, covered, or otherwise hidden from view.

viewWillAppear: Notifies the view controller that its view is about to be become visible.

viewDidUnload: It is called when the when the controller’s view is released from the memory.

I hope this article has helped you out, comments and queries are always welcome.
Happy iCoding and have a great Day

7 comments:

  1. Hello Sir,
    This info very helpful for clear my programming concept.


    Cheers,
    Umesh

    ReplyDelete
  2. This is Very Beautiful explanation.

    ReplyDelete
  3. This is very simple way to understand the life cycle of view and how they
    are called.But we want to know that ,what is hierarchy of calling of the
    above method.

    ReplyDelete
  4. nice tutorial !!!

    ReplyDelete
  5. very nicely explained
    thank you

    ReplyDelete