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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5368196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:17:57  来源:igfitidea点击:

How to create a simple checkbox in iOS?

iosiphonecheckbox

提问by Doom

Possible Duplicate:
Checkbox in IPhone application

可能重复:
iPhone 应用程序中的复选框

I want to create a simple checkbox with 2 values and save this, how can I make that?

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

Thanks.

谢谢。

回答by Gal Blank

Yeah, no checkbox for you in iOS (-:

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

Here, this is what I did to create a checkbox:

在这里,这是我创建复选框的操作:

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];

Now in the target method do the following:

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

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

That's it!

就是这样!

回答by JustSid

On iOS there is the switch UI component instead of a checkbox, look into the UISwitchclass. The property on(boolean) can be used to determine the state of the slider and about the saving of its state: That depends on how you save your other stuff already, its just saving a boolean value.

在 iOS 上有 switch UI 组件而不是复选框,查看UISwitch类。属性on(boolean) 可用于确定滑块的状态及其状态的保存:这取决于您已经保存其他内容的方式,它只是保存一个布尔值。