Saturday, March 14, 2015

IOS Developer - UIView Controller Lifecycle iOS 6 and onward

Hello everybody,

      Today I just would like to show you guys the new IOS 6 lifecycle for UIView. So, first let's go to the picture.




Then the explanation - there are six different stages which makes the lifecycle of a UIViewController. I've listed them according to the order. 
  1. loadView 
  2. viewDidLoad
  3. viewWillAppear
  4. viewWillLayoutSubviews
  5. viewDidLayoutSubviews
  6. viewDidAppear
Let me explain all those stages. 
1. loadView
This event creates the view that the controller manages. It is only called when the view controller is created programmatically. That means, it makes a good place to create your views in code. 
2. viewDidLoad
The viewDidLoad event is only called when the view is created and loaded in memory. But the bounds for the view are not defined yet. That means it's good place to initialize the objects that the view controller is going or can use.
3. viewWillAppear

This event communicate to the view controller 'saying' to it, when the view will appear on the screen. In this step the view has bounds that are defined but the orientation is not set. 
4. viewWillLayoutSubviews
If you are not using constraints or Auto Layout you probably want to update the subviews in here.
This is the first step in the lifecycle where the bounds are finalized. 
5. viewDidLayoutSubviews
Subviews have been setup and it notifies the view controller of it. It is a good place to make any changes to the subviews after they have been set. 
6. viewDidAppear
This viewDidAppear event comes just after the main view is presented on the screen.That makes it a perfect place to get data from a backend service or database using, for example JSON or connecting to NOSQL.

Happy coding,
Thiago.

No comments:

Post a Comment