The View of a View Controller

As subclasses of UIViewController, all view controllers inherit an important property:

    var view: UIView!

This property points to a UIView instance that is the root of the view controller’s view hierarchy. When the view of a view controller is added as a subview of the window, the view controller’s entire view hierarchy is added, as shown in Figure 4.2.

Figure 4.2  Object diagram for WorldTrotter

Object diagram for WorldTrotter

A view controller’s view is not created until it needs to appear on the screen. This optimization is called lazy loading, and it can conserve memory and improve performance.

There are two ways to create a view controller’s view:

  • in Interface Builder, by using an interface file such as a storyboard

  • programmatically, by overriding the UIViewController method loadView()

While the view controller’s view will be created using one of those two approaches, that view’s hierarchy may be created entirely in Interface Builder, entirely in code, or a mixture of both.

WorldTrotter’s view hierarchy is currently created in Interface Builder using a storyboard. You will continue to use Interface Builder in this chapter as you further explore view controllers. In Chapter 5, you will get experience creating programmatic views using loadView().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.16.184