vinay
vinay

Reputation: 361

I am using same action for 5 buttons and want to know which button is called?

I am using same action for 5 buttons and want to know which button is called

Upvotes: 0

Views: 84

Answers (4)

Praksha
Praksha

Reputation: 265

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
button.tag=1;
[view addSubview:button];

-(void)aMethod:(id)sender{

UIButton *button = (UIButton *)sender;

int clickedBtnTag = button.tag ;

Nslog("clicked button tag is %d",clickedBtnTag);

} 

Try this and then please revert me..

Upvotes: 0

Lalit
Lalit

Reputation: 264

use:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
button.tag=1;
[view addSubview:button];

then use:

-(void)aMethod:(id)sender{

UIButton *button = (UIButton *)sender;

int clickedBtnTag = button.tag ;

Nslog("clicked button tag is %d",clickedBtnTag);

} 

Try this and then please revert me..

Upvotes: 0

AppleDelegate
AppleDelegate

Reputation: 4249

In such a case please try assigning unique tags to the buttons.

In the target method regain the button tag as follows

Eg

-(void)targetMethod:(id)sender{

UIButton *button = (UIButton *)sender;

int clickedButtonTag = button.tag ;


} 

Upvotes: 4

Vishal
Vishal

Reputation: 8256

Give tag value to your button in nib then add this in your button action:

allbtn = sender;
    btntag = allbtn.tag;
    NSLog(@"btntag:%d",btntag);
    if(btntag==1)
    {
    } 

Simple now you easily find what button you tap.

Upvotes: 2

Related Questions