Jack Shultz
Jack Shultz

Reputation: 2081

How can I handle a button click on Apple Watch?

I have a button in my main interface. I did a ctl + drag from the button in the storyboard to the InterfaceController.m I then put an NSLog in the resulting method. This is what the code looks like

#import "InterfaceController.h"


@interface InterfaceController()

@end


@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
     NSLog(@"awakeWithContext!");
    // Configure interface objects here.
}

- (IBAction)Alarm {
    NSLog(@"Alarm!");

}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
     NSLog(@"willActivate!");
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
     NSLog(@"didDeactivate!");
}

@end

I also took a screenshot with the logs appearing. No log occurs when a I press the Alarm button in the emulator.

enter image description here

UPDATE

I just followed the example in this video. "How to connect a WKInterfaceButton to an action in the code using Xcode" https://www.youtube.com/watch?v=CuCuL-a608w

I added this to the header - (IBAction)alarmPressed:(id)sender; I put this in the view controller

- (IBAction)alarmPressed:(id)sender
{
     NSLog(@"alarmPressed!");
}

I dragged the sent action select to the Interface Controller and selected alarmPressed. This still does not produce logs when pressed. Also traits includes "User Interaction Enabled"

Upvotes: 2

Views: 1950

Answers (1)

Marc Smith
Marc Smith

Reputation: 349

Unfortunately the watchKit doesn't support the :(id)sender argument. so your IBAction should just read

-(IBAction)alarmPressed{
    NSLog(@"alarmPressed!"):
}

also make sure that this dot is filled in. if it isn't that means you need to click on the hollow dot and drag it to your button.

A Working outlet

Upvotes: 1

Related Questions