3

Can someone please tell me how can I change the title of the button ? I have 10 buttons with tags from 1 to 10 and I want to change the title of the clicked button when I click on it. It always change the title of the last button with tag 10. Buttons are connected by this:

@property (assign,nonatomic) IBOutlet NSButton *clickButton;

I also added synthesize in *.m file. In *.m file I have:

NSLog(@"Button pressed: %i", (int)[sender tag]); [clickButton setTitle:[NSString stringWithFormat:@"%c",'o']];

The method is also connected to the button:

  • (IBAction)startGame:(id)sender;

I do not want to make switch or make 10 buttons with no tags. Can I do this by any function to directly show text on the button which is clicked ?

  • You have 10 buttons and you want to change the title of button that's clicked or want to change the title of the last button ? What title you want to set to the clicked button ? – nkongara Dec 25 '13 at 00:13
  • I want to change the title of the button which is clicked. All buttons are connected to one method and one property. The title could be a text like "I was clicked" or a char "X". – user3133543 Dec 25 '13 at 00:37

2 Answers2

5

The sender argument to your -startGame: method will be the NSButton that was clicked. So you could just do this:

- (IBAction)startGame:(id)sender
{
    [sender setTitle:@"Title string"];
}
indragie
  • 18,002
  • 16
  • 95
  • 164
0

You need to connect all button action with same Action by doing so you well get the action at a common place with the clicked button object and by accessing the button you can change the title. :)

Anupam Gupta
  • 623
  • 1
  • 7
  • 18
  • if you have any problem while changing use below code. - (IBAction)startGame:(NSButton *)sender { [sender setTitle:@"Title string"]; } – Anupam Gupta Apr 14 '16 at 06:47