user2940137
user2940137

Reputation: 15

Apple Watch get button title on IBAction

I tried all the options for iPhone, to get the title of a button on click, but none of them works on Watch Simulator. So far, I tried:

• NSString *senderTitle = [sender titleForState:UIControlStateNormal];

• NSString *title = [(UIButton *)sender currentTitle];

• UIButton * button = (UIButton *)sender;
  NSString *title = [[button titleLabel] text];

• UIButton *button = (UIButton *)sender;
  NSString *title = button.currentTitle;

Any suggestions, please?

Upvotes: 2

Views: 2057

Answers (3)

Edison
Edison

Reputation: 11987

In WatchOS2 this is

buttonOutlet.setTitle("David Bowie")

and also note that you cannot change the button title color. For that you will need to create a an identical button with a custom title color in PS then swap the image using

setBackgroundImage(image: UIImage?)

Upvotes: 0

msweet168
msweet168

Reputation: 371

If you want to change the title of a button on a tap in WatchKit it is very simple.

-(IBAction)changebuttontitle:(id)sender {
[self.button setTitle:@"button title here"];
}

Upvotes: 0

dan
dan

Reputation: 9825

WatchKit doesn't include a sender with button actions and even if it did there is no way to get the current title from a WKInterfaceButton. If you need to know the title of the button you should have a different action for each of your buttons so you know which button was pressed to cause the action.

Upvotes: 2

Related Questions