如何使用for循环获取按钮标题

时间:2013-09-03 05:53:21

标签: ios objective-c

        //here   take a array with letters 
        _englishArray = [[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
        int i=0;
        float x=5,y=13;
        //add array elements as button title
        for (i=0; i< [_englishArray count]; i++) {
            button.tag=i+1;
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
            button.frame= CGRectMake(x, y, 29, 30);
            button.titleLabel.textColor =[UIColor whiteColor];
            x= x+31;
            if (x==320 || x>310) {
                x=5;
                y=y+40;
            }
            [_keyboardImageView addSubview:button];
        }


        //   and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code...
    //here get the button title useing for loop

   for (int j=0;j<=[_englishArray count]; j++) {
   //  here get button from image view by useing tag
            butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
            NSLog(@"%@",butt.titleLabel.text);
        }

它打印null不给按钮标题............ 注意:我也在使用button.currentitle 怎么可能..为什么它的显示为空?

3 个答案:

答案 0 :(得分:1)

由于您使用setTitle:forState:设置了标题,因此请使用titleForState:来获取标题:

NSLog(@"title is %@", [butt titleForState:UIControlStateNormal]);

当然,这假定butt不是nil。分配按钮后,您需要指定按钮的标签:

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i+1;

答案 1 :(得分:0)

确保将按钮定义为for循环,如

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

并指定标签。

   button.tag=i+1;

您必须在for循环中定义按钮而不是.h文件。

获取标题文字

      for (int j=0;j<=[_englishArray count]; j++) {
  UIButton  *butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
    NSLog(@"%@",butt.titleLabel.text);
}
希望它会对你有所帮助。

答案 2 :(得分:0)

_englishArray = [[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
int i=0;
float x=5,y=13;
//add array elements as button title
for (i=0; i< [_englishArray count]; i++) {
    button.tag=i+1;
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

    button.frame= CGRectMake(x, y, 29, 30);
    [button setBackgroundColor:[UIColor grayColor]];
    [button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
    //button.titleLabel.textColor =[UIColor whiteColor];
    [_keyboardImageView addSubview:button];
    x= x+31;
    if (x==320 || x>310) {
        x=5;
        y=y+40;
    }

}

//here get the button title useing for loop
//   and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code....
for (int j=0;j<=[_englishArray count]; j++) {
    butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
    NSLog(@"Required Result is %@",butt.titleLabel.text);


}

尝试一下,它会起作用......