美文网首页
iOS UIButton按钮单选,多选,去除选择。

iOS UIButton按钮单选,多选,去除选择。

作者: 夜冰雨 | 来源:发表于2021-11-24 11:09 被阅读0次

利用Objective-C撸了个小玩意,先看一下效果。

多选 单选+去除

以下是代码部分:

- (void)viewDidLoad {

    [super viewDidLoad];

    for(NSIntegeri =0; i <5; i++) {

            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];

            btn.frame=CGRectMake(10,100+50*i +10*i,100,50);

            [btnsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            [btnsetTitle:[NSString stringWithFormat:@"%ld",i] forState:UIControlStateNormal];

            btn.tag=1000+i;

            [btnsetBackgroundColor:[UIColor orangeColor]];

            [btnaddTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

            [self.viewaddSubview:btn];

    }

}

//多选

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

    sender.selected= !sender.isSelected;

    if(sender.selected) {

        NSLog(@"选中");

        [sendersetBackgroundColor:[UIColor redColor]];

    }else{

        NSLog(@"未选中");

        [sendersetBackgroundColor:[UIColor orangeColor]];

    }

}

//单选和去除

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

    sender.selected= !sender.isSelected;

    for(NSIntegeri=0; i<5; i++) {

        if(sender.tag== i+1000) {

            if(sender.selected) {

                [sendersetBackgroundColor:[UIColor redColor]];

            }else{

                [sendersetBackgroundColor:[UIColor orangeColor]];

            }

            continue;

        }

        UIButton *btn = (UIButton *)[self.view viewWithTag:i + 1000];

        btn.selected=NO;

        [btnsetBackgroundColor:[UIColor orangeColor]];

    }

}

相关文章

网友评论

      本文标题:iOS UIButton按钮单选,多选,去除选择。

      本文链接:https://www.haomeiwen.com/subject/xdrntrtx.html