Question

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)

Was it helpful?

Solution 3

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.

OTHER TIPS

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top