Setting the Initial View Controller

Although a storyboard can have many view controllers, each storyboard file has exactly one initial view controller. The initial view controller acts as an entry point into the storyboard. You are going to add and configure another view controller to the canvas and set it to be the initial view controller for the storyboard.

Open Main.storyboard. From the library, drag a View Controller onto the canvas (Figure 4.3). (To make space on the canvas, you can zoom out by Control-clicking on the background, using the zoom controls at the bottom of the canvas, or using pinch gestures on your trackpad.)

Figure 4.3  Adding a view controller to the canvas

Adding a view controller to the canvas

You want this view controller to display an MKMapView – a class designed to display a map – instead of the existing white UIView.

Select the view of the new View Controller – not the View Controller itself! – and press Delete to remove this view from the canvas. This might be easier to do using the document outline. Then drag a Map Kit View from the library onto the view controller to set it as the view for this view controller (Figure 4.4).

Figure 4.4  Adding a map view to the canvas

Adding a map view to the canvas

Now select the new View Controller and open its attributes inspector. In the View Controller section, check the Is Initial View Controller checkbox (Figure 4.5).

Figure 4.5  Setting the initial view controller

Setting the initial view controller

Did you notice that the gray arrow on the canvas that was pointing at the conversion view controller is now pointing to the new view controller? The arrow, as you have probably surmised, indicates the initial view controller. Another way to assign the initial view controller is to drag that arrow from one view controller to another on the canvas.

Build and run the application. Because you have changed the initial view controller, the map shows up instead of the view of the temperature conversion screen.

As mentioned above, there can only be one initial view controller associated with a given storyboard. When you set the new map view controller to be the initial view controller, the conversion view controller was no longer the initial view controller for this storyboard. Let’s take a look at how this requirement works with the root-level UIWindow to add the initial view controller’s view to the window hierarchy.

UIWindow has a rootViewController property. When a view controller is set as the window’s rootViewController, that view controller’s view is added to the window’s view hierarchy. When this property is set, any existing subviews on the window are removed and the view controller’s view is added to the window with the appropriate Auto Layout constraints.

Each application has one main interface, a reference to a storyboard. When the application launches, the initial view controller for the main interface is set as the rootViewController of the window.

The main interface for an application is set in the project settings. With the project navigator open, click the WorldTrotter project at the top of the list to open the project settings. In the General settings tab, find the Deployment Info section. Here you will see the Main Interface setting (Figure 4.6). It is set to Main, which corresponds to Main.storyboard.

Figure 4.6  An application’s main interface

An application’s main interface
..................Content has been hidden....................

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