0

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

4

4 回答 4

4

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

在目标方法中重新获得按钮标签如下

例如

-(void)targetMethod:(id)sender{

UIButton *button = (UIButton *)sender;

int clickedButtonTag = button.tag ;


} 
于 2013-02-12T06:24:18.907 回答
2

为 nib 中的按钮赋予标签值,然后将其添加到按钮操作中:

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

现在很简单,您可以轻松找到您点击的按钮。

于 2013-02-12T06:23:35.610 回答
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);

} 

试试这个,然后请回复我..

于 2013-02-12T11:04:33.310 回答
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);

} 

试试这个,然后请回复我..

于 2014-09-15T06:15:16.420 回答