如何在iOS中创建一个简单的复选框?

时间:2011-03-20 11:30:22

标签: ios iphone checkbox

  

可能重复:
  Checkbox in IPhone application

我想创建一个包含2个值的简单复选框并保存,我该怎么做?

感谢。

2 个答案:

答案 0 :(得分:120)

是的,iOS中没有你的复选框( - :

这是我创建复选框时所做的:

UIButton *checkbox;
BOOL checkBoxSelected;
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(x,y,20,20)];
// 20x20 is the size of the checkbox that you want
// create 2 images sizes 20x20 , one empty square and
// another of the same square with the checkmark in it
// Create 2 UIImages with these new images, then:

[checkbox setBackgroundImage:[UIImage imageNamed:@"notselectedcheckbox.png"]
                    forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
                    forState:UIControlStateSelected];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
                    forState:UIControlStateHighlighted];
checkbox.adjustsImageWhenHighlighted=YES;
[checkbox addTarget:(nullable id) action:(nonnull SEL) forControlEvents:(UIControlEvents)];
[self.view addSubview:checkbox];

现在在目标方法中执行以下操作:

-(void)checkboxSelected:(id)sender
{
    checkBoxSelected = !checkBoxSelected; /* Toggle */
    [checkbox setSelected:checkBoxSelected];
}

就是这样!

答案 1 :(得分:34)

在iOS上有切换UI组件而不是复选框,查看UISwitch类。 属性on(布尔值)可以用来确定滑块的状态以及状态的保存:这取决于你如何保存其他东西,它只是保存一个布尔值。