Search code examples
iosobjective-cuiviewuiviewcontrollerviewdidload

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?


Solution

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