Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between view controller life cycle and view life cycle in iOS?

Reading several tutorials and books on iOS development I often encounter terms : UIViewController life cycle and UIView life cycle. I'm interested: is there an actual difference between these two terms? Looking Apple docs I found that methods such as viewDidAppear or loadView is a part of view controller's life cycle, but I think it is more correct to refer them as view life cycle and methods such as initWithNibName or dealloc as controller's life cycle. Or there is no such a separation and when someone speaks about view life cycle he actually means UIViewController life cycle?

like image 724
MainstreamDeveloper00 Avatar asked Feb 25 '15 17:02

MainstreamDeveloper00


3 Answers

Both are different concepts, therefore have different life cycles.

UIViewController

A ViewController is the Controller in a MVC architecture. Therefore, it is responsible to coordinate the info between the model (your data) and your views. UIViewControllers coordinate the UIViews and are part of navigation between screens (pushViewController, presentViewController). Therefore it needs to know when it will appear in the screen.

A UIViewController has a very specific life Cycle and it has methods that can be extended that are part of that life cycle. Examples of those methods are:

viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear

View

A View, on the other hand, shouldn't be worried when it has to appear on screen. Therefore, it has a complete different life cycle:

awakeFromNib, willMoveToSuperView, didMoveToSuperView

This methods usually are called in the sequence of the UIViewController's life cycle. Therefore, normally, the UIView responds to changes and people don't consider they have a Life cycle on their own.

The ViewController's life cycle only makes sense when a class extends UIViewController while a UIView's Life cycle only makes sense when extends UIView.

Most of the times, when people talk about Life cycle they will talk about the UIViewController life cycle, since the View usually responds to changes. Two examples of those changes are: the view changed its size, changes its parent.

like image 139
Tiago Almeida Avatar answered Oct 13 '22 01:10

Tiago Almeida


All your confusion will go away once you fully realise the difference between the two classes and ingrain it into your mind (by practice)

UIViewController - a class that has no UI in itself (not completely true though...see root view), its purpose is to control views and do some related stuff..It is like a policeman or traffic controller policing others. (views). Most of the time you create your own subclass of UIViewController and that class tends to be quite heavy on functionality like

  • handling logical rules when to show what view
  • connecting to model layer (data and facts about the problem your app is solving)
  • interacting with other controllers,

UIView - a class that represents a rectangle area that can be heavily visually modified, but the most important fact is, it is visible on the screen, and can have subviews, which are also UIViews. Views are organised into view hierarchies. Most of the time you customize your view so that it is

  • visually pleasing
  • handles it's subviews via autolayout
  • represents the particular type of visual information you need often subclassed to a more specific view class like labels, texts, buttons, etc.

One bit that confuses newcomers is that every view controller has one root view, a property which holds a UIView instance. Often you can get lost as to whether this root view is discussed, or the view controller is discussed. In causal discussion between developers, the two words are sometimes used interchangeably.


Both controllers & views have the lifecycle but you must not confuse the two.

ViewController lifecycle is what happens to the controller itself, like it awakes from nib file, or receives a low memory warning, but mostly about how its root view comes to life, how it appears disappears and dies..

View Lifecycle is about how the view lays out its subviews, and how it renders its content.


I like visual analogies.. Simply imagine a policeman with a lot of colorful paper rectangles. The policeman is the controller, and he says what (views) is shown and when on the screen.

The controller and the View are abstractions that are part of (Model View Controller) MVC architectural pattern. I recommend you study that immediately, so that the problem with lifecycle confusion is further cleared in your mind.

like image 31
Earl Grey Avatar answered Oct 13 '22 01:10

Earl Grey


Adding to these answers and focusing a bit more on your exact questions:

I'm interested: is there an actual difference between these two terms?

Yes, there is a different between both, but in a context where you are dealing with a UIViewController's root UIView, they are in a way related.

Looking Apple docs I found that methods such as viewDidAppear or loadView is a part of view controller's life cycle, but I think it is more correct to refer them as view life cycle and methods such as initWithNibName or dealloc as controller's life cycle.

And this is why they are related:

  • viewWillAppear
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear:

They are all under Responding to View Events in the documentation. So these are callbacks from the UIView to the UIViewController telling it about its state. And although:

  • loadView
  • viewDidLoad

Are not under the same section (in the documentation), it's the UIView requesting/telling the UIViewController about its current state. And you have other examples of these interactions, for example:

  • viewWillLayoutSubviews
  • viewDidLayoutSubviews

In a way (and in your question's context) the general answer is: yes, those are two different lifecycles with different particularities, but they are related in a way. An example where they are not directly related would be the UIViewController's didReceiveMemoryWarning. (I say directly, because indirectly, it might be the UIViewController's root UIView the culprit).

like image 2
Rui Peres Avatar answered Oct 13 '22 01:10

Rui Peres