您的位置:首页 > 移动开发 > IOS开发

iOS 设置Button的 初始选中状态

2015-11-16 15:22 477 查看
进入页面列表页面的时候可能 会有二级列表,数据默认是二级列表的第一个栏目下的数据,所以,默认情况下第一个 应该是选中的状态
比如活动秀 就是 这个二级列表的第一个,下面数据就是它的数据,所以他显示的是选中状态 红色的。

btnMutableArray = [[NSMutableArray alloc]init]; //将button放到数组里面

NSMutableDictionary *titleDic = [[NSMutableDictionary alloc]init];
//这个是二级标题的所有信息的字典 首页返回的

for (int i = 0; i < titleMutableArray.count;
i++) {

titleDic = [titleMutableArray objectAtIndex:i];

allButton = [[UIButton alloc]initWithFrame:CGRectMake(viewWidth/4*i,
0, viewWidth/4, 35)];

[allButton setTitle:titleDic[@"name"] forState:UIControlStateNormal];

classid = [titleDic[@"id"]intValue];

allButton.tag = classid;

allButton.titleLabel.font =
[UIFont systemFontOfSize:14];

[allButton setTitleColor:RGBCOLOR(231,
96, 73) forState:UIControlStateSelected];

[allButton setTitleColor:RGBCOLOR(51,
51, 51) forState:UIControlStateNormal];

allButton.titleLabel.textAlignment = NSTextAlignmentCenter;

[allButton addTarget:self action:@selector(fashionButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[headerScrollerView addSubview:allButton];

[btnMutableArray addObject:allButton];

}

((UIButton *)[btnMutableArray objectAtIndex:0]).selected=YES; //
关键是这里,设置 数组的第一个button为选中状态

//点击事件

-(void)fashionButtonPressed:(UIButton *)sender{

[myFashionMutableArray removeAllObjects];

[_myfashionTableView reloadData];

((UIButton *)[btnMutableArray objectAtIndex:0]).selected=NO;
//点击其他button之后这里设置为非选中状态,否则会出现2个红色的选中状态

if (sender != allButton) {

allButton.selected = NO;

allButton = sender;

} //这个if是网上抄的,有点神奇

allButton.selected = YES;

_fashionpid = sender.tag;

pageSize = 0;

[self loaddataMethodsWithpid:_fashionpid
page:pageSize number:Baby_NumofList andSord:0];



}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: