一、for循环定义UIButton,默认选中第一个
<span style="font-size:18px;">-(void)oilChoice
{
for (int i=0; i<3; i++) {
oilChoice=[[UIButton alloc] initWithFrame:CGRectMake(15+i*40, 15,30,50)];
[oilChoice addTarget:self action:@selector(oilChoiceAction:) forControlEvents:UIControlEventTouchUpInside];
[oilChoice setTitle:product.OilName forState:UIControlStateNormal];
[oilChoice setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[oilChoice setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
oilChoice.backgroundColor=[UIColor colorWithRed:234.0/255 green:234.0/255 blue:234.0/255 alpha:1];
[_smallScrollView addSubview:oilChoice];
[allButtonArray addObject:oilChoice];
}
((UIButton *)allButtonArray.lastObject).selected=YES;
}
-(void)oilChoiceAction:(UIButton *)btn
{
//button的选择状态
((UIButton *)allButtonArray.lastObject).selected=NO;
if (btn!=oilChoice) {
oilChoice.selected=NO;
oilChoice=btn;
}
oilChoice.selected=YES;
}
</span>