×

iOS UIView LifeCycle


What is UIViewController ?

The word UIViewController itself tells the meaning. A Controller which controls or manages an UI of View. This is the place where you can add as many views you want.

What is UIView ?

UIView is a place which tells your iPHone's full screen to give me some part of the screen so that i can define it in my own way(say)

Example :- A part of screen with height 60.

UIViewController's view Life Cycle :-

1) viewDidLoad - It is automatically called when the view controller is loaded completely in the memory. Initialisation of Objects is done here.

So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.

Example :-

viewDidLoad is used to initialize setup of your interface or when you want to create database access or when you want to prepopulate the data etc.


2) viewWillAppear - This is called every time the view is about to appear, whether or not the view is already in memory. Put your dynamic code here, such as model logic.

Example :-

1) Update navigation bar style
2) Update status bar style
3) Update screen data
4) Orientation handling


3) viewDidAppear - Put expensive operations here that you only want to do if you're sure the view is onscreen, such as network calls.It is called when the view controller's view was added to the view hierarchy.

Example :-

1) Start UI animation
2) Display loader view


4) viewWillDisappear - It is called when the view controller's view is about to be removed from the view hierarchy.

Example :-

1) Hide keyboard
2) Revert changes that were made in viewWillAppear method.

5) viewDidDisappear - It is called when the view controller's view was removed from the view hierarchy.

Example :-

1) Remove cache data.