iOS 设置Button的 初始选中状态

进入页面列表页面的时候可能 会有二级列表,数据默认是二级列表的第一个栏目下的数据,所以,默认情况下第一个 应该是选中的状态 iOS <wbr>设置Button的 <wbr>初始选中状态
比如活动秀   就是 这个二级列表的第一个,下面数据就是它的数据,所以他显示的是选中状态 红色的。


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];

 

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值