![]() ![]() Override viewWillAppear(_:) if you need the configuration to be done each time the view controller’s view appears onscreen. Which should you choose? Override viewDidLoad() if the configuration only needs to be done once during the run of the app. This method is called just before a view controller’s view is added to the window. The second option is another UIViewController method, viewWillAppear(_:). This method is called after the view controller’s interface file is loaded, at which point all the view controller’s outlets will reference the appropriate objects. ![]() The first option is the viewDidLoad() method that you overrode to spot lazy loading. So where can you access a subview? There are two main options, depending on what you need to do. Often, you will want to do some extra initialization or configuration of subviews defined in Interface Builder before they appear to the user. At this point, both views have been loaded, so switching between the tabs now will no longer trigger viewDidLoad(). Tap MapViewController’s tab, and the console will report that its view is now loaded. The console reports that ConversionViewController loaded its view right away. If the console is not visible, open the Debug area from the button in the top-right corner of Xcode or by using the keyboard shortcut, Command-Shift-Y. Listing 4.1 Creating a new view controller subclass ( MapViewController.swift) import Foundation import UIKit class MapViewController: UIViewController īuild and run the application and check out the console ( Figure 4.17). In MapViewController.swift, define a UIViewController subclass named MapViewController. Now that you want to add code to the view controllers, you are going to create two view controller subclasses and associate them with their respective interface.Ĭreate a new Swift file (Command-N) and name it MapViewController. However, there is no code currently associated with either view controller, because everything has been configured using the storyboard. You are going to add code to both view controllers. 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. The map view controller’s view is not needed and will only be needed when (or if) the user taps the tab to see it. ![]() When the application launches, the tab bar controller defaults to loading the view of the first view controller in its array, which is the temperature conversion view controller. Now that you have two view controllers, the lazy loading of views mentioned earlier becomes more important. Learn More Buy Loaded and Appearing Views IOS Programming: The Big Nerd Ranch Guide, 7th Edition ![]()
0 Comments
Leave a Reply. |