objective-c 制作 UIButton 复选框的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2227366/
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-09-03 22:54:45  来源:igfitidea点击:

What is the best way to make a UIButton checkbox?

iphoneobjective-ccheckboxuibutton

提问by cduck

I am trying to make a standard check box for my iPhone app from a UIButtonwith a title and image. The button image changes between an "unchecked" image and a "checked" image.

我正在尝试为我的 iPhone 应用程序创建一个UIButton带有标题和图像的标准复选框。按钮图像在“未选中”图像和“选中”图像之间变化。

At first I tried subclassing UIButtonbut UIButtonhas no -init***method to use in my -initmethod.

起初我尝试了子类化,UIButton但在我的方法中UIButton没有-init***方法可以使用-init

What is the best way to do this?

做这个的最好方式是什么?

Thanks in advance.

提前致谢。

回答by Jasarien

You shouldn't need to subclass the UIButtonclass. By design, Objective-C favors composition over inheritance.

您不应该需要对类进行子UIButton类化。按照设计,Objective-C 更喜欢组合而不是继承。

UIButtonis a subclass of UIControl, which has a selectedproperty. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitchdoes.

UIButton是 的子类UIControl,它有一个selected属性。您可以使用此属性来切换复选框的开/关行为,就像 aUISwitch一样。

You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:

您可以将一个动作附加到按钮的触摸内部事件,并在那里执行切换,如下所示:

// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];

- (void)myCheckboxToggle:(id)sender
{
    myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}

回答by emp

Set the images in the button:

在按钮中设置图像:

[button setImage:uncheckedImage forState:UIControlStateNormal]
[button setImage:checkedImage forState:UIControlStateSelected]

Now all you need to do is:

现在你需要做的就是:

button.selected = state

and the correct images will display.

并会显示正确的图像。

回答by chown

All you need to do is set 2 different images for the states UIControlStateNormaland UIControlStateSelected, then in your selector, changed the selectedproperty of the button.

所有你需要做的是设置2幅不同的图像的状态UIControlStateNormalUIControlStateSelected,然后在你的选择,改变了selected按钮的属性。

Here is a working example (replace image names with your own):

这是一个工作示例(用您自己的图像名称替换图像名称):

- (void)loadView {
    // ...
    UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [chkBtn setFrame:CGRectMake(0, 0, 300, 25)];

    [chkBtn setImage:[UIImage imageNamed:@"UNCHECKED.png"] 
            forState:UIControlStateNormal];
    [chkBtn setImage:[UIImage imageNamed:@"CHECKED.png"]
            forState:UIControlStateSelected];

    [chkBtn addTarget:self 
               action:@selector(chkBtnHandler:) 
     forControlEvents:UIControlEventTouchUpInside];

    // Optional title change for checked/unchecked
    [chkBtn setTitle:@"I am NOT checked!"
            forState:UIControlStateNormal];
    [chkBtn setTitle:@"I am checked!"
            forState:UIControlStateSelected];

    [self.view addSubview:chkBtn];
    [chkBtn release], chkBtn = nil;
    // ...
}

- (void)chkBtnHandler:(UIButton *)sender {
    // If checked, uncheck and visa versa
    [sender setSelected:!sender isSelected];
}

回答by Brayden

For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:

对于对未来感兴趣的任何人 - 而不是自己做,只需从 GitHub 下载下面的链接,它已经从 UIControl 子类化,并且可以完美地用作复选框。还包括一个关于它是多么容易使用的示例项目:

https://github.com/Brayden/UICheckbox

https://github.com/Brayden/UICheckbox

回答by neoneye

I have used M13Checkbox in one of my projects. Works ok.

我在我的一个项目中使用了 M13Checkbox。工作正常。

https://github.com/Marxon13/M13Checkbox

https://github.com/Marxon13/M13Checkbox

回答by Gank

    UIImage* nonCheckedImage=[UIImage imageNamed:@"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
    UIImage* CheckedImage=[UIImage imageNamed:@"ic_check_box_black_48dp.png"];//[UIImage init

    //ic_check_box_black_48dp.png
    [_checkBox setImage:CheckedImage forState:UIControlStateSelected];
    [_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
}

- (IBAction)checkBoxToggle:(id)sender {
    _checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL

}

the image you can use google icon

您可以使用谷歌图标的图像

回答by King

Try this:-

尝试这个:-

-(IBAction)clickCheckButton:(UIButton *)sender {

    if (sender.tag==0) {

    sender.tag = 1;

    [sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 

    }else

    {

    sender.tag = 0;

    [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;

    [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

    } 
}

回答by Jeff R

Why not use a switch - UISwitch? This is used to display an element showing the boolean state of a value.

为什么不使用开关 - UISwitch?这用于显示显示值的布尔状态的元素。

回答by willcodejavaforfood

Did you try overriding the initWithCoder method, just in case it is loaded from a nib somehow?

您是否尝试覆盖 initWithCoder 方法,以防万一它以某种方式从笔尖加载?