How do I know which button is pressed? Objective C

I'm doing Presence1 in Assignment, which requires me to create a multi-screen app. I have two ViewController, vc1 and vc2. In vc1, I have two buttons. I use the same method for them and their name is the same.

My question is, how can I find out which button is pressed in vc1 when I go to vc2?

There is a theme to show me that I have to get the (x, y) positions of the mouse when I click the button, but I think this is not very good.

+2


source to share


2 answers


The answer above will work. If you don't want to store the outputs for buttons, you can tag them in the interface builder. As an example, you assign button 1 the value of tag 1 and button 2 the value of tag 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
    }
}

      

+4


source


If you have two properties NSButton

for example:

@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 to the button pointer:



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

    else if (sender == stopButton) {
    }
}

      

This is what you need?

+1


source







All Articles