- Navigation Styles on the Apple Watch
- Navigating Using Segues
- Navigating Programmatically
- Summary
Navigating Programmatically
You can also perform page navigation by using code. In the Storyboard Editor, select the second Interface Controller and set its Identifier attribute to Page2 (see Figure 12). You'll use this identifier when navigating programmatically.
Figure 12 Setting the identifier for an Interface Controller.
In the InterfaceController.swift file, create two actions for the Hierarchical and Page-based buttons (you can use the Show Assistant editor button to help you create the actions):
import WatchKit import Foundation class InterfaceController: WKInterfaceController { @IBAction func btnHierarchical() { } @IBAction func btnPagebased() { } @IBOutlet weak var label: WKInterfaceLabel!
Add the bold statements in the following code to the two actions:
import WatchKit import Foundation class InterfaceController: WKInterfaceController { @IBAction func btnHierarchical() { pushControllerWithName(“Page2”, context: [“segue”: “hierarchical”, “data”:“Passed through hierarchical navigation”]) } @IBAction func btnPagebased() { presentControllerWithName(“Page2”, context: [“segue”: “pagebased”, “data”: “Passed through page-based navigation”]) } @IBOutlet weak var label: WKInterfaceLabel!
The pushControllerWithName:context: method performs a hierarchical navigation equivalent to using the push segue. The context argument allows you to pass data to the destination Interface Controller. You navigate to the specific Interface Controller you want by using its identifier.
The presentControllerWithName:context: method performs a page-based navigation equivalent to using the modal segue. The context argument allows you to pass data to the destination Interface Controller.
When you run the application, you'll get the same behavior as demonstrated in the previous discussion.
For the presentControllerWithName:context: method, another method is very similar—the presentControllerWithNames:contexts: method. Instead of displaying another page modally, this method allows you to display a series of pages. To see how this works, add a Swift page to the HelloAppleWatch WatchKit Extension group of the project and name it Page3.swift (see Figure 13).
Figure 13 Adding the new Page3.swift.
Populate Page3.swift as follows:
import WatchKit import Foundation class Page3: WKInterfaceController { override init(context: AnyObject?) { super.init(context: context) } override func willActivate() { super.willActivate() } override func didDeactivate() { super.didDeactivate() } }
In the storyboard, add another Interface Controller, add a Label view to it, and set its caption to Page 3 (see Figure 14).
Figure 14 Adding a new Interface Controller.
Set the Class attribute of the Interface Controller to Page3 (see Figure 15).
Figure 15 Setting the class for the newly added Interface Controller.
Set the Identifier attribute of the Interface Controller to Page3 (see Figure 16).
Figure 16 Changing the identifier for the Interface Controller.
Add the bold statements in the following code to the InterfaceController.swift file:
@IBAction func btnPagebased() { presentControllerWithNames( [“Page2”, “Page3”], contexts: [ [“segue“: “pagebased”, “data”: “Passed through page-based navigation”], [“segue”: “pagebased”, “data”: “Passed through page-based navigation”]]) }
In this case, you're calling the presentControllerWithNames:contexts: method with the following arguments:
- An array of strings containing the identifiers of the Interface Controllers to display.
- An array of objects to pass to the destination Interface Controllers; the first object will be passed to the first Interface Controller specified in the first argument, and so on.
Now you can run the project again. Clicking the Page-based button reveals Page 2 (see the middle of Figure 17). You can swipe to the left to reveal Page 3 (at right in Figure 17). Clicking either Close or Cancel returns to the main Interface Controller.
Figure 17 Displaying multiple pages at once in page-based navigation.