2014-02-26 2 views
0

내가하려고하는 것이 가능한지 이해할 수 없습니다.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]; 

    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]; 
} 

및 방법 touchButton에 내가 마지막 루프에서 터치 버튼

-(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 

    } 
} 

의 태그를 얻을 후 내가 가지고있는 버튼을 선택합니다 :

나는 루프에 버튼을 만드는 오전 건드리지 않은 태그 (이 경우 3,2,1)

가능하지 않습니까?

덕분에 모두

답변

5

당신이 필요로 그렇게 같은 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]; 
    } 
} 
+0

덕분에, 나는 시도하고 작동한다! 감사합니다 – Ilario

+0

@Ilario : 답변을 수락하는 것을 잊지 마세요 ... 나는 당신이 10 분을 기다리고 있다고 생각합니다;) –

+0

@llario 신난다, 다행입니다! – Eric

관련 문제