IOS 根据数组的个数对UIButton进行重复或循环使用
//设置一个view
view = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 320, 30)];
view.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:view];
for (int i=0; i<5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = 100+i;
button.frame = CGRectMake(64*i+3, 3, 58, 25);
[button addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:@"%@",[array objectAtIndex:i]] forState:UIControlStateNormal];
//默认在第一个
if (button.tag == 100) {
[button setBackgroundImage:[UIImage imageNamed:@"blueBtn.png"] forState:UIControlStateNormal];
}
//将循环创建的button都添加到view上面
[view addSubview:button];
}
#pragma mark - button触发的方法
-(void)doButton:(UIButton *)sender
{
for (UIButton *button in view.subviews) {
if (button.tag != sender.tag) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
else
[button setBackgroundImage:[UIImage imageNamed:@"blueBtn.png"] forState:UIControlStateNormal];
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。