Declaration

Overview

An interface controller serves the same purpose as a UIViewController object in a UIKit app, except that it does not manage any actual views. It runs in your WatchKit extension and remotely manages the behavior associated with an interface controller in your Watch app’s storyboard file. You subclass WKInterfaceController and use its methods to configure the elements of your storyboard scene and to respond to interactions with those elements.

Your interface controller code runs locally on the user’s Apple Watch but is separate from the interface that it manages. When you change the value of an interface object in your code, the system forwards the needed information to your Watch app, which makes the corresponding changes onscreen.

Initialize Your Interface Controllers

When the user interacts with your app content, the system launches your extension and creates the appropriate interface controller objects automatically. Apps use different interface controllers to manage their notification and app interfaces; WatchKit uses the information in your app’s main storyboard file to determine which interface controller to load. Notification scenes are configured specially so that the system can identify them. For your app, WatchKit loads your app’s main interface controller initially, but you may change the initial interface controller at launch time.

When creating an interface controller, WatchKit instantiates the class and calls its init() method. You can use this method to initialize variables and load data; however, don't use it to configure your user interface. The controller's user interface elements may not be properly initialized when this method runs.

Next, the system calls the awake(withContext:) method. If WatchKit passes a valid object to the awake(withContext:) method, use the information in that object to customize the initialization process. Also, the controller's user interface elements are guaranteed to be available at this point. This means that you can safely use this method to configure your user interface.

The willActivate() method lets you know when your interface is about to become active. Use the willActivate() method to perform any last minute tasks, such as checking for updates to your content; however, don't use it for your primary initialization.

The willActivate() method may be called at times when your interface is not yet onscreen. For example, WatchKit may call the method in advance so that you have time to update your content. WatchKit calls the didAppear() method to let you know when your interface becomes visible. Similarly, WatchKit calls the willDisappear() and didDeactivate() methods when your interface moves offscreen again.

Important

An interface controller can make changes to its interface only in the awake(withContext:) method, in the willActivate() method, and while the interface is active. Once the system calls the didDeactivate() method, it ignores any attempts to change the value of the controller’s interface objects until the system calls the interface controller’s willActivate() method again.

In iOS Simulator, WatchKit calls the didDeactivate() method for the current interface controller when you lock the simulator by selecting Hardware > Lock. When you subsequently unlock the simulator, WatchKit calls that interface controller’s willActivate() method again. You can use this capability to debug your activation and deactivation code.

Interface Builder Configuration Options

Xcode lets you configure information about your interface controller in your storyboard file. Table 1 lists the attributes you can configure in your storyboard and their meaning.

Table 1

Interface controller attributes

Attribute

Description

Identifier

The name of the interface controller. Use this name to specify which interface controller to push or present.

Title

The title string assigned to the interface controller. You can set this value programmatically using the setTitle(_:) method.

Is Initial Controller

A Boolean indicating whether the object is the app’s root interface controller. Only one interface controller at a time may have this option enabled. This option does not apply to glance or notification interface controllers.

Activity Indicator On Load

A Boolean indicating whether the interface controller’s contents should be hidden until the willActivate() method returns. When this option is enabled, the system displays a progress indicator until the willActivate() method returns. You might disable this option if your interface contains mostly static information that can be displayed right away.

Always Bounce

A Boolean value that turns off scrolling and allows built-in controls and containers to fill content to the screen edges, regardless of the content-safe area.

Full Screen

A Boolean value that determines whether SpriteKit or SceneKit content can use the full screen. The system hides the status bar but displays the time in the upper-right corner with a gradient behind it, making the time clearly visible against the scene.

Fixed to screen edges

A Boolean value that indicates whether content should ignore the safe area and minimum layout margins. When this option is enabled, the system turns off scrolling, and allows built-in controls and containers to fill content to the screen edges.

The content mode for the background image. This mode defines how the background image scales or fills the screen and behaves in the same way as the constants for the UIView.ContentMode type.

Animate

A Boolean value indicating whether an animated background image starts running its animation automatically after being loaded. Set this option to Yes if you want the animation to start automatically; set it to No if you prefer to start the animation programmatically.

Color

The background color to be displayed behind the scene’s content.

Insets

The amount of space (in points) to insert between the edges of the interface controller and its content. Selecting Custom lets you specify different values for the top, bottom, left, and right edges.

Spacing

Additional spacing (in points) to include between items in the interface controller.

Subclassing Notes

Subclass WKInterfaceController when you have a storyboard scene that requires configuration at runtime or that handles user interactions. Typically, you define a custom subclass for each unique storyboard scene that your app manages. In your subclass, define outlets for any interface objects you need to configure and define action methods for responding to interactions with the elements of your storyboard scene.

Most custom interface controllers you use in your app require a custom interface controller subclass. Even glances need an interface controller to update the glance contents. The only storyboard scene that cannot use a custom interface controller is the scene associated with a static notification interface. When implementing an interface controller for your dynamic notification interface, subclass WKUserNotificationInterfaceController instead.

Override any methods of the class needed to configure your interface and get it ready to display. Most interface controllers override the init() and awake(withContext:) methods. Override any other methods that make sense based on your needs.