我对5个按钮使用相同的动作,想知道哪个按钮被调用?

时间:2013-02-12 06:20:38

标签: iphone ios5 ibaction

我对5个按钮使用相同的操作,想要知道哪个按钮被称为

4 个答案:

答案 0 :(得分:4)

在这种情况下,请尝试为按钮分配唯一标签。

在目标方法中重新获得按钮标记,如下所示

例如

-(void)targetMethod:(id)sender{

UIButton *button = (UIButton *)sender;

int clickedButtonTag = button.tag ;


} 

答案 1 :(得分:2)

为nib中的按钮添加标记值,然后在按钮操作中添加:

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

现在您很容易找到您点按的按钮。

答案 2 :(得分:0)

使用:

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);

} 

试试这个,然后请还原我..

答案 3 :(得分:0)

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);

} 

试试这个,然后请还原我..