views:

304

answers:

2

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.

+1  A: 

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?

Nick Bedford
Oh, if i replace NSButton with UIButton, it will be ok? There are some problems...
Khoa
The example above should be very similar with regards to Cocoa (Mac SDK) and Cocoa touch (iPhone SDK)
Nick Bedford
A: 

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
    }
}
Elfred
Wow, it's very simple. I didn't know that. But there was something wrong, i received a error : "Request for member 'tag' in something not a structure or union". Do i have to do something else?
Khoa
Ahh, sorry about that, since sender is an id, you can't use property notation. use [sender tag] instead or change the signature to -(void) onButtonClick:(UIButton*) sender.
Elfred