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

4

10

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?

Antitoxin answered 25/2, 2015 at 17:20 Comment(0)
R
18

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.

Rhearheba answered 25/2, 2015 at 17:39 Comment(0)
R
22

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.

Rost answered 25/2, 2015 at 17:24 Comment(3)
In general, your answer is correct, but a bit outdated. Starting from iOS 6 UIViewController never purge its view from memory under low-memory conditions. In particular, methods viewWillUnload and viewDidUnload are never called. Documentation.Pilcher
The lifecycle these days is pretty much just that the view is loaded the first time that UIViewController.view is called, added and removed from the view hierarchy any number of times and deallocated sometime during or after [UIViewController dealloc] when it is already outside of the view hierarchy.Spongioblast
You are right but I couldn't find a good updated diagram. Maybe I should remove it, but I believe is better to visualize an outdated diagram than not understand the connections. I was trying to find a good diagram for UIView as well (connecting to an UIVIewController diagram) but couldn't find it :(. You guys think it is better to remove this diagram?Rost
R
18

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.

Rhearheba answered 25/2, 2015 at 17:39 Comment(0)
E
2

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).

Edwardoedwards answered 25/2, 2015 at 23:27 Comment(0)
P
0
Most of the lifecycle is handled automatically by the 
system and no need for calling some methods like parent and child view 
controllers relationship
Here are the mentioned lifecycle methods for UIView:
didAddSubview(_:)
willRemoveSubview(_:)
willMove(toSuperView:)
didMoveToSuperview
willMove(toWindow:)
didMoveToWindow
Polacre answered 27/4, 2021 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.