A button in the user interface of your watchOS app.
- iOS 8.2+
- watchOS 2.0+
The content of a button's tappable area is filled with text and an optional background color or image. When tapped by the user, the button calls its associated action method, which you define on the owning interface controller. Use that action method to initiate tasks and update your app’s interface.
Do not subclass or create instances of this class yourself. Instead, define outlets in your interface controller class and connect them to the corresponding objects in your storyboard file. For example, to refer to a button object in your interface, define a property with the following syntax in your interface controller class:
During the initialization of your interface controller, WatchKit creates a new instance of this class and assigns it to your outlet. At that point, you can use the object in your outlet to make changes to the onscreen button.
To respond to taps in the button, declare a method of this form in the interface controller class that manages the button:
You can change the name of your action method to anything you like. In your Xcode storyboard, connect the button’s selector to the custom action method defined in your class.
Using Buttons in watchOS 1
In watchOS 1, WatchKit coalesces the data from all setter method calls made during the same run loop iteration and transmits that data to the user’s Apple Watch at the end of the run loop. If you set an attribute to different values in the same run loop iteration, only the last value is transmitted. If you set an attribute to the same value in the same run loop iteration, WatchKit generates a log message so that you can track down the duplicate change.
Images set using the
set methods must be sent wirelessly from the user’s iPhone to the Apple Watch before they can be displayed. You can minimize this latency by caching images prior to use and setting them using the
set method instead. In watchOS 2, images are already on the device and do not need to be sent wirelessly.
Interface Builder Configuration Options
Xcode lets you configure information about your button interface object in your storyboard file. Table 1 lists the attributes you can configure in your storyboard and their meaning.
The type of content contained in the button. A button can contain a single text label or a group. For buttons containing a group, you can add text, images, and other objects to the group.
The color to apply to the button’s title.
The font to apply to the button’s title. You can set font information programmatically using the
A checkbox indicating whether the button is enabled and sends events when tapped. You can also configure this value programmatically using the
The background color for the button.