gpt4 book ai didi

ios - 上一个单选按钮状态未清除 objective-c

转载 作者:行者123 更新时间:2023-11-29 05:40:53 25 4
gpt4 key购买 nike

我想要什么?

自定义单选按钮,允许用户在多个选项中选择一个选项

代码

 - (void)setupBtn{
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,18,18)];
[self.radioBtn setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:@selector(radioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view2 addSubview:self.radioBtn];

}
- (IBAction)radioButtonTapped:(UIButton*)sender{
if (self.radioBtn.isSelected == true) {
[self.radioBtn setSelected:NO];
self.view1.backgroundColor =[UIColor clearColor];
[self.radioBtn setImage:[UIImage imageNamed:@"radio-button"] forState:UIControlStateNormal];
}else {
[self.radioBtn setSelected:YES];
self.view1.backgroundColor = [UACFCustomColors continentalLightBlueTransparent];
[self.radioBtn setImage:[UIImage imageNamed:@"radio-button-select"] forState:UIControlStateSelected];
}
}

最佳答案

尝试将所有按钮存储在一个数组中。当一个按钮被点击时,循环遍历数组,并检查是否每个按钮都被选中。如果该按钮被选中,则将其设置为不选中,然后退出循环。循环结束后,将点击的按钮 (sender) 设置为选中状态。

未经测试的示例代码:

- (void) viewDidLoad {
self.radioBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
[self.radioBtn setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn setSelected:YES];
[self.view2 addSubview:self.radioBtn];

self.radioBtn2 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn2 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn2 setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
[self.radioBtn2 setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn2 addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn2 setSelected:NO];
[self.view2 addSubview:self.radioBtn2];

self.radioBtn3 = [UIButton buttonWithType: UIButtonTypeCustom];
[self.radioBtn3 setFrame:CGRectMake(40,38,20,20)];
[self.radioBtn3 setImage: [UIImage imageNamed:@"radio-button"]forState:UIControlStateNormal];
[self.radioBtn3 setImage: [UIImage imageNamed:@"radio-button-select"]forState: UIControlStateSelected];
[self.radioBtn3 addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.radioBtn3 setSelected:NO];
[self.view2 addSubview:self.radioBtn3];
self.buttonArray = @[self.radioBtn, self.radioBtn2, self.radioBtn3];

}

- (void) myButtonTapped:(UIButton*) sender {
for(UIButton *btn in self.buttonArray) {
// Only one button should be selected at a time, so once we find it,
// set it to not be selected then break from the loop
if(btn.isSelected){
[btn setSelected:NO];
break;
}
}
// set the button that was tapped to be selected
[sender setSelected:YES];
}

将一个按钮的文本发送到另一个按钮的文本相当简单。

[btn2 setTitle:[btn1 titleForState:UIControlStateNormal] forState:UIControlStateNormal];  

关于ios - 上一个单选按钮状态未清除 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com