我正在使用for循环创建一个九个按钮,我想将按钮存储在一个数组中,因为我想一次访问所有按钮。
for(i = 0; i < 9; i++ )
{
num1 = [UIButton buttonWithType:UIButtonTypeCustom];
[num1 setExclusiveTouch:YES];
[num1 setTag:tag_start+i];
num1.tag = currentTag;
currentTag++;
[num1 setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
[num1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[num1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[num1.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:35.0]];
//Store the button in array
[numBtnArray addObject:num1];
NSLog(@"%d", numBtnArray.count); //it show 0 nothing in that array
if(i >= 5) {
num1.frame = CGRectMake(430, i*60-160, 50, 50);
}
else {
num1.frame = CGRectMake(350, i*60+140, 50, 50);
}
[num1 addTarget:self action:@selector(buttonFunction:) forControlEvents:UIControlEventTouchUpInside];
[num1 setBackgroundImage:[UIImage imageNamed:@"greyBtn.png"] forState:UIControlStateNormal];
[num1 setBackgroundImage:[UIImage imageNamed:@"greyBtn.png"] forState:UIControlStateHighlighted];
[self.view addSubview:num1];
}
答案 0 :(得分:0)
我不明白要求是什么,但如果你需要将它们存储在一个数组中,只需要
NSMutableArray *btnArray = [NSMutableArray new];
for(i = 0; i < 9; i++ )
{
// ...
[btnArray addOjnect:num1];
[self.view addSubview:num1];
}
答案 1 :(得分:0)
更改代码如下
numBtnArray = [[NSMutableArray alloc] init];
for(i = 0; i < 9; i++ ) {
UIButton *num1 = [UIButton buttonWithType:UIButtonTypeCustom];
//...
//Store the button in array
[numBtnArray addObject:num1];
NSLog(@"%d", numBtnArray.count);
//...
}
答案 2 :(得分:0)
目前尚不清楚你想要什么,但我建议你不要将按钮存储在数组中,因为UIView
已经这样做了。
如果您想对按钮执行某种质量更改,那么您可以:
因此,如果您想更改self.view
中按钮的背景颜色,您只需:
for (id obj in self.view.subviews) {
if (![obj isKindOfClass:[UIButton class]])
continue;
UIButton *button = (UIButton *)obj;
button.backgroundColor = [UIColor blueColor];
}