2011-05-16 6 views
0

안녕하세요,iPhone SDK의 사용자 정의 버튼 액션 문제

내 응용 프로그램에서 scrollView에 목록을 표시하고 있습니다. scrollView의 각 항목 에 대해 사용자 정의 btn을 배치했습니다. btn 작업에서 선택되지 않은 이미지를 Selected Image로 변경해야합니다. 그러나 처음 btn을 클릭하면 마지막 btn 이미지가 변경됩니다.

코드 :

- (void)reLoadView 
     { 

    float yCoordinateStartLocation = 20; 
    float xCoordinateStartLocatioForItem = 80; 
    float xCoordinateStartLocatioForButton=30; 
    float questionWidth =100; 
    UIFont* questionFont = [UIFont boldSystemFontOfSize:19]; 
    UIColor *color = [UIColor colorWithRed:0.0 green:54.0/255.0 blue: 129.0/255.0 alpha:1.0]; 
    UIColor *bgcolor = [UIColor clearColor]; 

    for(int i=0; i<[itemArray count]; i++) 
    { 
    showItems *showItem = [itemArray objectAtIndex:i]; 

    float itemHeight = [ShowViewController calculateHeightOfTextFromWidth:showItem.itemName :questionFont : questionWidth :UILineBreakModeCharacterWrap]; 


    itemLabel=[[UILabel alloc]init]; 
    itemLabel.frame=CGRectMake(xCoordinateStartLocatioForItem,yCoordinateStartLocation,questionWidth,40); 
    itemLabel.text =showItem.itemName; 
    itemLabel.textColor = color; 
    itemLabel.font = questionFont; 
    itemLabel.numberOfLines = 0; 
    itemLabel.lineBreakMode = UILineBreakModeCharacterWrap; 
    itemLabel.textAlignment=UITextAlignmentLeft; 
    itemLabel.backgroundColor = bgcolor; 
    [scrollView addSubview:itemLabel]; 


    UIImage *unSelectedImage = [UIImage imageNamed:@"unChecked.png"]; 
    checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName tagValue:tagCount]; 
    //checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName]; 

    CGRect frame = CGRectMake(xCoordinateStartLocatioForButton,yCoordinateStartLocation, 30, 30); 
    [checkBoxBttn setFrame:frame]; 
    checkBoxBttn.tag=tagCount; 


    [checkBoxBttn setBackgroundImage:unSelectedImage forState:UIControlStateNormal]; 
    [checkBoxBttn setShowsTouchWhenHighlighted:YES]; 
    [checkBoxBttn addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [scrollView addSubview:checkBoxBttn]; 
    tagCount++; 


    yCoordinateStartLocation = yCoordinateStartLocation + itemHeight + 20; 

    } 

     scrollView.contentSize = CGSizeMake(320, yCoordinateStartLocation+50); 
     } 



     -(void)checkButtonAction:(MyCustomButton*)sender 
{ 
    NSString *sndStr = [sender itemName]; 
    //taglabel=[sender tagNumber]; 
    //NSString *tagNo=[NSString stringWithFormat:@"%d",taglabel]; 



    for(int i=0; i<[itemArray count]; i++) 
    { 
    showItems *showItem = [itemArray objectAtIndex:i]; 


    if([sndStr isEqualToString:showItem.itemName]) 
    { 
      printf("\n ====  checkBoxBttn.tag:%d", checkBoxBttn.tag); 
    UIImage *selectedImage = [UIImage imageNamed:@"checked.png"]; 
    [checkBoxBttn setBackgroundImage:selectedImage forState:UIControlStateNormal]; 

    } 
    } 

    } 

은 누구도 날이에 도움이 될 수 있습니다. 미리 감사드립니다.

+0

코드에서 checkBoxBttn을 정의하는 곳은 어디입니까? –

답변

0

여기에서 성취하려는 것을 파악하지 못했습니다. 어떤 시점에서 UITableView가 사용자의 요구에 적합 할 수도 있다고 생각했습니다. 만약 -checkButtonAction:에 다시 참조 할 때

처음 버튼 checkBoxBttn-reLoadView에서 루프의 끝에 최종 버튼에 설정된 점이다 누를 때 마지막 단추가 변하는 이유는 그렇게 마지막 버튼을 말하며 사용자 동작을받은 버튼이 아닙니다.

당신은 사용자 동작을 수신 버튼의 이미지를 변경하고자한다고 가정하면, 다음과 같은 코드를 사용 할 수 있습니다 - 문제 을

-(void)checkButtonAction:(MyCustomButton*)sender 
{ 
    UIImage *selectedImage = [UIImage imageNamed:@"checked.png"]; 
    [sender setBackgroundImage:selectedImage forState:UIControlStateNormal]; 
} 
+0

물론 이것은 뒤집기위한 것이 아닙니다. 뒤집기에는 추가 작업이 필요합니다. –

0

이 줄을 [checkBoxBttn setBackgroundImage : selectedImage forState : UIControlStateNormal];

태그를 사용하여 checkBoxBttn 배경 이미지를 업데이트해야합니다.

관련 문제