안녕하세요,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];
}
}
}
은 누구도 날이에 도움이 될 수 있습니다. 미리 감사드립니다.
코드에서 checkBoxBttn을 정의하는 곳은 어디입니까? –