Loaded and Appearing Views

Now that you have two view controllers, the lazy loading of views mentioned earlier becomes more important. When the application launches, the tab bar controller defaults to loading the view of the first view controller in its array, which is the ConvertViewController. The MapViewController’s view is not needed and will only be needed when (or if) the user taps the tab to see it.

You can test this behavior for yourself. When a view controller finishes loading its view, viewDidLoad() is called, and you can override this method to make it print a message to the console, allowing you to see that it was called.

You are going to add code to both view controllers. However, there is no code currently associated with the view controller displaying the map because everything has been configured using the storyboard. Now that you want to add code to that view controller, you are going to create a view controller subclass and associate it with that interface.

Create a new Swift file (Command-N) and name it MapViewController. Open MapViewController.swift and define a UIViewController subclass named MapViewController.

import Foundation
import UIKit

class MapViewController: UIViewController {

}

Now open Main.storyboard and select the map’s view controller. Open its identity inspector and change the Class to MapViewController.

Now that you have associated the MapViewController class with the view controller on the canvas, you can add code to both ConversionViewController and MapViewController to print to the console when their viewDidLoad() method is called.

In ConversionViewController.swift, update viewDidLoad() to print a statement to the console.

override func viewDidLoad() {
    super.viewDidLoad()

    print("ConversionViewController loaded its view.")

    updateCelsiusLabel()
}

In MapViewController.swift, override the same method.

override func viewDidLoad() {
    super.viewDidLoad()

    print("MapViewController loaded its view.")
}

Build and run the application. The console reports that ConversionViewController loaded its view right away. Tap MapViewController’s tab, and the console will report that its view is now loaded. At this point, both views have been loaded, so switching between the tabs now will no longer trigger viewDidLoad(). (Try it and see.)

Accessing subviews

Often, you will want to do some extra initialization or configuration of subviews defined in Interface Builder before they appear to the user. So where can you access a subview? There are two main options, depending on what you need to do. The first option is the viewDidLoad() method that you overrode to spot lazy loading. This method is called after the view controller’s interface file is loaded, at which point all of the view controller’s outlets will reference the appropriate objects. The second option is another UIViewController method, viewWillAppear(_:). This method is called just before a view controller’s view is added to the window.

Which should you choose? Override viewDidLoad() if the configuration only needs to be done once during the run of the app. Override viewWillAppear(_:) if you need the configuration to be done each time the view controller’s view appears onscreen.

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

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