Pergunta

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 ?

Foi útil?

Solução

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"];
}

Outras dicas

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. :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top