改变继承UIButton的两个子类状态

来源:互联网 发布:手机虚拟摇杆软件 编辑:程序博客网 时间:2024/05/18 02:27
各位我在做四个按钮,刚刚开始的时候第一个按钮为选中状态,然后接下来点击哪个哪个按钮上的图片和字体颜色改变。可是我用下面的for循环出来了四个button可是它显示出来的状态都是位最后一个按钮为选中状态。这个是怎么回事呢?

-(void)loadCustomTabBarView{
    //初始化自定义TabBar背景
    UIImageView *imageBG = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_H-50, SCREEN_W, 49)];
    imageBG.backgroundColor = [UIColor lightGrayColor];
    imageBG.userInteractionEnabled = YES;
    [self.view addSubview:imageBG];
    
    //初始化自定义TabBar
    float coordinateX = 0;
    for (int i=0; i<4; i++) {
        button = [[UIButton alloc]init];
        button.frame = CGRectMake(20+coordinateX, 0, 49, 49);
        button.tag = i;
        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
        [imageBG addSubview:button];
        coordinateX +=75;
        
        //初始化自定义选中背景
        selectedView = [[UIImageView alloc]init];
        selectedView.frame = CGRectMake(7,4,27,27);
        selectedView.tag = i;
        selectedView.image = [UIImage imageNamed:[imageNameArray objectAtIndex:i]];
        [button addSubview:selectedView];
        
        label = [[UILabel alloc]init];
        label.frame = CGRectMake(0, 33, 49, 14);
        label.text = [tabName objectAtIndex:i];
        label.tag = i;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12.0f];
        [button addSubview:label];
    }
}

-(void)changeViewController:(UIButton *)sender{
    self.selectedIndex = sender.tag;
    label.textColor = RGB(251, 44, 10, 1);
    selectedView.image = [UIImage imageNamed:[imageSelectArray objectAtIndex:sender.tag]];
}

0 0
原创粉丝点击