Creating Interface Objects

Remember in the last chapter,, when you dragged your first interface object onto the blank canvas of your storyboard? That black area inside the Interface Builder canvas was your interface controller. Think of an interface controller like a view controller, its iOS equivalent; they both manage screenfuls of content—albeit a much smaller screen on the watch. When your WatchKit app loads, whichever interface controller in the storyboard is set to be the initial controller, signified by an arrow pointing to it, will display first. There are two ways to change which interface controller gets loaded first: dragging the arrow around in your storyboard or selecting the interface controller, navigating to Xcode’s Attributes Inspector (4), and checking the Is Initial Controller check box.

Every interface controller needs an associated class, and that class needs to be a subclass of WKInterfaceController. In our HelloWatch example, Xcode created an interface controller class named InterfaceController. Creative, right? Now you may be wondering how you create your own interface controllers. For iOS you have two options: in the storyboard or in code. Once you have a UIViewController subclass, it’s easy to do either way. WKInterfaceControllers in WatchKit, on the other hand, can’t be created in code; you can only create them in your WatchKit app’s storyboard.

In the last chapter when I mentioned that you would be creating all of your watch UI in your storyboard, I wasn’t just telling you best practices or leading you down the easiest path; you literally cannot create any interface objects—including interface controllers—in code. If you try to call InterfaceController to create a new interface controller in HelloWatch, your app will crash. Similarly, if you try to create a new interface object to put in your interface controller by calling, say, WKInterfaceLabel, the returned object will be nil! Because you can’t create interface objects programmatically, your storyboard must contain everything your app needs.

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

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