Make App Pie

Training for Developers and Artists

Swift WatchKit: Introducing Navigation to the Apple Watch(Part 1: Page Interfaces)

2015-05-20_07-27-01The Simulator for WatchKit is the worst part of Xcode. It’s horribly buggy, and has one particular bug that freezes or kills your running app very often. I had a hard time figuring this out, but once I found one place that discussed it, the number of people with the same issue surprised me. Apparently if something is wrong in your layout on a single interface app, the app fails and hangs. There is a solution: Use more than one interface scene, then run your app. This get us into the world of the three type of controllers you can use in WatchKit apps: hierarchical, Page-based and modal. Unlike iOS, you can’t use all three in your app. You have to choose for your app to be completely Hierarchical or Page-based, no mixing allowed. You are however allowed to use modals with both.

Page-Based Controllers

The easiest of the controllers is the page-based interface. Page interfaces are swiped from one view to another like their iOS counterpart:

watchkitPages2

Page views have difficulty not communicating information between views. They are for related views that do little to no communication with each other.

Unlike the iOS equivalent, which requires delegates and several views, they are very easy to set up. Let’s set up an example. In XCode, start a new single-view project named SwiftWatchPageDemo, with Universal as the device and Swift as the language. Once loaded in the drop down menu select Editor>Add Target.

2015-05-20_05-25-39

Select an Apple Watch  WatchKit App for this application.

2015-05-20_05-26-51

We will not use notifications. In the next window, make sure both glances and notifications are checked off.

2015-05-20_05-27-39

Click Finish and then Activate. In the navigator, find the Watchkit App group. Open the Interface.storyboard.

2015-05-20_05-29-35

You will have an empty storyboard with one controller. In the object inspector, find the Interface controller, which should be at the top of the list.

2015-05-20_05-31-14

Drag three interface controllers to the storyboard, placing them next to each other like this:

2015-05-20_05-35-50

Drag one label to each of the interfaces. Position them Center Vertically and Center Horizontally. Change the text in the label to match the screenshot.

2015-05-20_05-38-33

On the interface labeled One, click where the interface says interface. You will see the interface controller icon.

2015-05-20_05-39-35

Control-Drag from the controller icon to the interface labeled Two until it highlights.

2015-05-20_05-42-03

Release the mouse button and you will get a very short menu of segue options.

2015-05-20_05-43-03

Select next page, and we get the page segue:

2015-05-20_05-45-06

Repeat the process to link interface Two to Three, and Three to Four to get this:

2015-05-20_07-48-21

That’s all you need to do. Build and run. Swipe the watch screen and you change pages:

watchkitPages

Instead of segues, We can transition programmatically. Quit the simulator, and back in the storyboard delete the segues, but leave the controllers in place. Click on the view controller icon for the One interface, and in the attribute inspector find the Identifier. Change it to One. Incidentally you can give your pages titles. In the title field, add First Controller.

2015-05-20_05-58-31

Do the same for the Two interface. so it looks like this:

2015-05-20_05-58-52

Then do the same for the Three/Third and Four/Fourth controller.

2015-05-20_07-27-01

When done, add another interface to the left of the others.

2015-05-20_07-31-55

In the attributes inspector for this new interface change to this:

2015-05-20_06-48-25

WatchKit has no AppDelegate for initialization. We have to simulate one by making an initialization controller to load the pages. Press Command-N and create a new file named MainInterfaceController subclassing WKInterfaceController. When the file finishes saving, change the awakeWithContext method to this:

override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        // Configure interface objects here.
        WKInterfaceController.reloadRootControllersWithNames(["One","Two","Three","Four"],contexts: nil)
}

The class method reloadRootControllersWithNames loads the pages.The first parameter takes an array of interface identifiers represented as strings. The order is significant. The first element will be the first page displayed and so on.

Go back to the storyboard. Select the main controller, and in the Identity inspector, change its class to MainInterfaceController.

2015-05-20_07-00-02

Build and run. Once again we get pages we can swipe through.

watchkitPages2

Pages require swiping, and give you little choice of which page to go to next. You could get complicated and change the order of pages with reloadRootControllersWithNames in a button’s code and several re-arranged arrays. But that is messy and complicated. There is an easier way which handles data better: Hierarchical interfaces. In our next lesson, we’ll look at these interfaces.

 

2 responses to “Swift WatchKit: Introducing Navigation to the Apple Watch(Part 1: Page Interfaces)”

  1. […] WatchKit  gives you a choice when  it comes to navigation. You can be Page-based as we introduced in our last lesson. Another alternative, is hierarchical interfaces, which closely resemble navigation controllers on […]

  2. […] cannot mix a page-based interface with a hierarchical (i.e. navigation) interface as we learned in previous lessons. You can use modal interfaces with either.  In this series of lessons we’ll look at using […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: