开发者

How to get the button index value in iPhone?

I have created the buttons are dynamically. Now i want to get the button index value.

Here my code is, For Eg, My Array,

 {
    one, 
    two,
    three,
    Four
 }

 int x = 10;

 for(int i = 0; i < [myArray count]; i++) {

 UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 [answerBtn setF开发者_运维技巧rame: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];
}

Expected result is,

  The btn tag is 1.


NSLog(@"The btn tag is %d", answerBtn.tag);


First of all you need to set the button tag to get it.

for(int i = 0; i < [myArray count]; i++) {

 UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 answerBtn.tag = i;

 [answerBtn setFrame:CGRectMake(30, x, 260, 40)];

 [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 button index, In this case i want to return the tag is 1.
  NSLog(@"The btn tag is %d", answerBtn.tag); // it's not w
 }



[self.view addSubview: answerBtn];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜