문제

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)

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top