rd42
rd42

Reputation: 3594

How to get the name of the button in an action?

I have a button named start and I want to know in the method that it calls what it's name is and I'm not really sure how to do it. This is the method the button calls.

-(IBAction) startMotion: (id)sender {
    UIButton * buttonName = (UIButton *) sender;
    NSLog(@"Button Name:  %@", buttonName.currentTitle);
}

The NSLog prints

Button Name: (null)

Upvotes: 0

Views: 953

Answers (3)

rd42
rd42

Reputation: 3594

I was using the wrong property in Interface Builder.I was using name property of button in Interface Builder instead of the title property from the button settings.

Upvotes: 1

Ilker Baltaci
Ilker Baltaci

Reputation: 11789

I would rather set a certain Tag and compare the tag value rather than reading the title of the button since you have possibility to localize your app where button titles will possibly be different.

Upvotes: 2

saurb
saurb

Reputation: 685

You can set the title of the button through

[b setTitle:@"Start" forState:UIControlStateNormal];

and to get the title (currentTitle is read-only and may be nil):

[b currentTitle];

BTW, if you just want to differentiate multiple buttons, you can just set the tag property (an integer value) of the buttons.

Also, check if you have the button specified as an IBOutlet in your viewController class, and is it connected properly as an outlet in Interface Builder?

Upvotes: 4

Related Questions