gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:39:58 26 4
gpt4 key购买 nike

        //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怎么可能..为什么它显示为空?

最佳答案

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

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

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

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

关于ios - 如何使用for循环获取按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com