views:

458

answers:

3

I'm a newbie just begun with iPhone and Obj-C for a month now.

I have two buttons, both of them call the same function as follows

[play addTarget:self action:@selector(showQstn:) forControlEvents:UIControlEventTouchUpInside];

Inside the function showQstn, I want to know what button was tapped. Any idea? Alternate ideas are welcome too.

TIA

A: 

You get the clicked button as the argument to your IBAction method showQstn:. You might consider setting the tag on the buttons, then using [sender tag] to identify which one triggered the IBAction method.

Tom Dalling
+3  A: 

Assuming that showQstn: is declared as

-(void) showQstn:(id)sender;

The argument sender is the trigger object of the event.

EDIT: You can also distinguish between the buttons using the tag property. Each view has a unique tag identifier (which can be set programatically or through Interface Builder).

EDIT: UIButton does not have a title property, hence you getting the error. It has a titleForState:. So you should change your code to the following:

NSString *title = [sender titleForState:UIControlStateNormal];
if ([title isEqualToString:@"PLAY!"]) {
  name.text = title;
}
notnoop
A: 

thanks for your replies. I'm still unable to make it work. My code is given below.

    if( [[sender title] isEqual:@"PLAY!"] ){
 qstn = [sender title];
 name.text = qstn;
}

here, qstn is NSString and name is UILabel. My application crashes at [sender title] line. In the console I found this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIRoundedRectButton title]: unrecognized selector sent to instance 0x5294d0

Anything wrong with 'CGRectMake' that the button was made of?

thanks in advance, your previous answers were helpful.

pMan
please check my updated answer. Also, you should edit your question rather than adding an "answer".
notnoop
sure.. thanks..!
pMan