0
        //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也在使用它怎么可能..为什么它显示为 null?

4

3 回答 3

1

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

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

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

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i+1;
于 2013-09-03T06:00:29.220 回答
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);


}

试试吧,它会工作的......

于 2013-09-03T06:11:27.683 回答
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);
}

希望它会帮助你。

于 2013-09-03T06:02:27.257 回答