Class

WKInterfaceButton

A button in the user interface of your watchOS app.

Declaration

@interface WKInterfaceButton : WKInterfaceObject

Overview

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:

@property (weak, nonatomic) IBOutlet WKInterfaceButton* myButton;

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:

- (IBAction)buttonAction

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.

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.

Table 1

WatchKit button attributes

Attribute

Description

Content

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.

Title

The title string assigned to the interface controller. You can also set this value programmatically using the setTitle: or setAttributedTitle: method.

Color (Button)

The color to apply to the button’s title.

Font

The font to apply to the button’s title. You can set font information programmatically using the setAttributedTitle: method.

Enabled

A checkbox indicating whether the button is enabled and sends events when tapped. You can also configure this value programmatically using the setEnabled: method.

Background

The background image to display in the button. You can also set this value programmatically using the setBackgroundImage:, setBackgroundImageData:, or setBackgroundImageNamed: method.

Color (Background)

The background color for the button.

Topics

Setting the Button Title

- setTitle:

Sets the button title to the specified string.

- setAttributedTitle:

Sets the button title to the specified attributed string.

Setting the Button Background

- setBackgroundColor:

Sets the background color of the button.

- setBackgroundImage:

Sets the button’s background image to the specified image.

- setBackgroundImageData:

Sets the button’s background image to the image in the specified data object.

- setBackgroundImageNamed:

Sets the button’s background image to the image in the named resource file.

Enabling and Disabling the Button

- setEnabled:

Enables or disables the button.

Relationships

Inherits From

See Also

Controls

WKInterfaceLabel

An interface element that displays static text.

WKInterfaceDate

A label that displays the current date or time.

WKInterfaceTimer

A label that displays a countdown or count-up timer.

WKInterfaceAuthorizationAppleIDButton

A button that you can use to trigger a Sign In with Apple request.

WKInterfacePaymentButton

A button that you can use to trigger payments through Apple Pay.

WKInterfaceTextField

An interface element that displays an editable text area.

WKInterfaceSwitch

An interface element that toggles between an On and Off state.

WKInterfaceSlider

An interface element that lets users select a single floating-point value from a range of values.

WKInterfaceActivityRing

A view that displays data from a HealthKit activity summary object.

WKInterfaceMap

An interface element that displays a noninteractive map for the location you specify.