gpt4 book ai didi

ios - 如何在 iOS 中创建复选框?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:16 25 4
gpt4 key购买 nike

我已经采用了两个自定义按钮和一种方法来设置它们的选中和取消选中

在 viewdidload() 中我是这样写的..

[checkbox_01 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO; //declared as boolean to change check and uncheck for button one
isChecked_02 = NO; //declared as boolean to change check and uncheck for button two

在方法中

-(IBAction)checkboxOnClick:(UIButton *)sender {
if (sender.tag == 1) {
if (isChecked_01 == NO) {
[checkbox_01 setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
isChecked_01 = YES;
}
else
{
[checkbox_01 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO;
}
}

if (sender.tag == 2) {
if (isChecked_02 == NO) {
[checkbox_02 setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
isChecked_02 = YES;
}
else
{
[checkbox_02 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
isChecked_02 = NO;
}
}
}

还有其他方法可以在 ios 中创建复选框吗?我已经为每个复选框取了一个 isChecked bool 值。是否有可能只对整个检查列表使用一个 bool 值......

最佳答案

您可以使用 selected属性(继承自 UIControl)而不是你的 isChecked_01 和 isChecked_02:

[checkbox_01 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];

[checkbox_01 setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[checkbox_02 setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

- (IBAction)checkboxOnClick:(UIButton *)sender {
sender.selected = !sender.selected;
}

关于ios - 如何在 iOS 中创建复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29974554/

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