Tuesday, October 29, 2013

ViewController LifeCycle

We had previously discussed ViewController in the topic Introduction to the ViewControllers in iOS and in that topic we had given an overview of the ViewController LifeCycle.

Today we are going to discuss only the ViewController LifeCycle and How we should use each of the methods provided in the lifecycle event.

What is LifeCycle?

A lifecycle is an event which has certain steps from the point of creation to deletion. So how do we know about the steps during this period? A Sequence of methods is called as they progress through the LifeCycle. 

Now we may need to perform a 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 happened 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. We will discuss each of the methods in detail.

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 is 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 maybe 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 a good place to put your setup code for the View. Always call the super class method to complete the lifeCycle methods. At this point in 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 the 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. The animated argument is again to tell whether are disappearing instantly or via an animation.

We might not want to write code that's 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 'viewappeared' 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.

Keep reading and Commenting....

Happy Coding!!!

No comments:

Post a Comment