我已經創建了動態的按鈕。現在我想獲得按鈕索引值。如何獲得iPhone中的按鈕索引值?
這裏我的代碼, 對於例如, 我的數組,
{
one,
two,
three,
Four
}
int x = 10;
for(int i = 0; i < [myArray count]; i++) {
UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[answerBtn setFrame:CGRectMake(30, x, 260, 40)];
answerBtn.tag = i;
[answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
NSString *actualString = @"two";
NSString *getString = [answerList objectAtIndex:i];
if([actualString isEqualToString:getString])
{
//How do i get the correct values of array index, In this case i want to return the index value is 1. Bcoz the array[1] = two.
NSLog(@"The btn tag is %d", answerBtn.tag); // couldn't get correct index
}
[self.view addSubview: answerBtn];
}
預期的結果是,
The btn tag is 1.
你真的設置了標籤嗎? – 2011-05-23 14:28:39
@Richard J Ross,是的,我編輯了我的問題,請現在看問題。 – Pugal 2011-05-23 14:30:50
@Pugal我看不到你試圖從哪個按鈕獲取標籤。 'answerBtn'的標籤總是等於'i'。 – 2011-05-23 14:33:24