Ios 获取UIButton的标签以及带有较小标签的标签

Ios 获取UIButton的标签以及带有较小标签的标签,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我不明白我想做的是可能的还是不可能的 我正在为循环创建按钮: CGRect rect2 = CGRectMake(50, 230, 40, 40); for (int i = 0; i<5; i++) { NSString *stringI = [NSString stringWithFormat:@"%d",i+1]; NSString *stringItouch = [NSString stringWithFormat:@"%dselected",i+1];

我不明白我想做的是可能的还是不可能的

我正在为循环创建按钮:

CGRect rect2 = CGRectMake(50, 230, 40, 40);

for (int i = 0; i<5; i++) {

    NSString *stringI = [NSString stringWithFormat:@"%d",i+1];
    NSString *stringItouch = [NSString stringWithFormat:@"%dselected",i+1];

    UIButton *button = [[UIButton alloc] init];
    [button setBackgroundImage:[UIImage imageNamed:stringI] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:stringItouch] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = i+1;

    button.frame = rect2;
    rect2.origin.x = rect2.origin.x + 45;

    [scrollView addSubview:button];
}
在最后一个循环中,我想选择标签小于触摸标签的按钮(在本例中为3,2,1)

有没有可能


谢谢大家

您只需要查看标签:像这样:

-(void)touchButton:(id)sender {

   UIButton *buttonSender = sender;
   buttonSender.selected = YES;
   NSLog(@"button tag %@",buttonSender.tag);

   for (int i = buttonSender.tag-1; i>0; i--) {

      NSLog(@"int = %d",i); 
      //for example if buttonSender.tag is 4, in this way i have 3,2,1

      /* Add this line */
      UIButton *tempButton = (UIButton *)[scrollView viewWithTag:i];
   }
}

您所需要的就是
viewWithTag:
像这样:

-(void)touchButton:(id)sender {

   UIButton *buttonSender = sender;
   buttonSender.selected = YES;
   NSLog(@"button tag %@",buttonSender.tag);

   for (int i = buttonSender.tag-1; i>0; i--) {

      NSLog(@"int = %d",i); 
      //for example if buttonSender.tag is 4, in this way i have 3,2,1

      /* Add this line */
      UIButton *tempButton = (UIButton *)[scrollView viewWithTag:i];
   }
}

谢谢你的回答,我试过了,效果很好!thanks@Ilario:别忘了接受答案。。。我想你在等10分钟;)@FahimParkar我得再等几分钟才能接受!;)谢谢你的回答,我试过了,效果很好!thanks@Ilario:别忘了接受答案。。。我想你在等10分钟;)@FahimParkar我得再等几分钟才能接受!;)