Khoa
Khoa

Reputation: 163

How can i know which button is clicked? Objective C

I am doing Presence1 in Assignment which require me builds a multiple screens application. I have two ViewController, vc1 and vc2. In vc1, i have two buttons. I use a same method for them and the title of them are same.

My question is How can i know which button is clicked in vc1 when i change to vc2?

There are a topic show me that I should get positions (x,y) of mouse when i click on button, but i think it is not quite good.

Upvotes: 2

Views: 5034

Answers (2)

Elfred
Elfred

Reputation: 3851

The answer above would work. If you don't want to keep outlets for the buttons you can assign them tags in interface builder. As an example, you assign button 1 a tag value of 1 and button 2 a tag value of 2. Then in the code

-(void)onButtonClick:(id)sender {
    if(sender.tag == 1) {
        //respond to button 1
    } else if(sender.tag == 2) {
        //respond to button 2
    }
}

Upvotes: 4

Nick Bedford
Nick Bedford

Reputation: 4445

If you have two NSButton properties such as:

@interface ViewControllerOne : NSViewController
{
    NSButton *goButton;
    NSButton *stopButton;
}

@property(nonatomic, retain) NSButton *goButton;
@property(nonatomic, retain) NSButton *stopButton;

-(void)onButtonClick:(id)sender;

@end

Then you can compare the sender with the button pointer:

-(void)onButtonClick:(id)sender
{
    if (sender == goButton) {
    }

    else if (sender == stopButton) {
    }
}

Is that what you're after?

Upvotes: 1

Related Questions