Life cycle of UIViewController in iphone

What is LifeCycle?

LifeCycle is an event which have certain steps from the point of creation to deletion. So how do we know about the steps during this period? A Sequence of methods are called as they progress through the LifeCycle.
Now we may need to perform different kind of actions at different steps of the life cycle and thus we commonly override these methods and perform these actions.
As I said the Start of LifeCycle is Creation. Most of the MVCs are Instantiated through the StoryBoard. So what happen after the creation of the ViewController?
  • Outlet setting
  • View Appear and Disappear
  • Changes in the  Geometry
  • Memory Warnings
At each of the above step iOS invokes methods on the viewControllers.

ViewController LifeCycle Methods


This is Strictly not part of the viewController lifeCycle but still play a role in the initialization of the viewController. This method is called on all the objects which come out of the StoryBoard. This happens before all the outlet are set i.e. before the loading of the view. Before you put any code in here you must think if you can put it somewhere else may be viewDidLoad or viewWillAppear. 
Note: Init method is not called on the objects which came out of the StoryBoard. So you might want to put the code written in the Init method in the awakeFromNib method too.


– (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
This is good place to put your setup code for the View. Always call the super class method to complete the lifeCycle methods. At this point of time of LifeCycle we are not sure of the Geometry of the Device so you do not put any code that is based on the Geometry of the view.


– (void)viewWillAppear:(BOOL)animated
Notification to the viewController when view is just about to View on the screen. animated argument tells whether you are appearing Instantly or after sometime via an animation. This method can be overridden to change the color or something else of the status bar according to the orientation or as per the style of the view.
Note: As you understand that view will be loaded once, however it will appear and disappear again and again. So you do not want to write a piece of code in this method which actually belongs in viewDidLoad, otherwise you will be doing the same thing again and again.


– (void)viewWillDisappear:(BOOL)animated
Notify the controller that view is about to be removed from the screen. This is the place you will write code like storing the state of the view or Cleaning up the resources being used. animated argument is again to tell whether are are disappearing instantly or via an animation.
We might not want to write code thats time consuming. We can kick off some thread which can do the task in the background.
There are did versions of both the viewWillAppear and viewWillDisapper methods viewDidAppear and viewDidDisappear which are called after the view is appeared and view is disappeared relatively.


– (void)viewWillLayoutSubviews
This method is invoked when frame changed its layout and subviews were this re-layout. In this method you can reset the view controls to set for the new layout. However, with iOS 6 and later we do not need this. AutoLayout take care of these layout changes and we can add constraints to the application and this will happen automatically.


– (void)didReceiveMemoryWarning
This method is rarely called. But if you are doing memory consuming tasks in your application like playing with the large images or playing sounds etc. than you should consider handling this method. To avoid memory warning anything which is memory consuming and can be recreated should be released means you should set the pointer to nil.
Note: Remember you never know what is the amount of memory available to your application and you should always keep your memory usage to as low as you can. You should not write you code considering you get lot of memory in the heap.
These is no unload anymore…. and thats it… Thats the lifecycle of the view controller.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s