在 Xcode 中使用 Storyboard 创建复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37809332/
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
Create Checkbox using Storyboard in Xcode
提问by
I am new to Objective-C so i am mostly using Storyboard, Someone Please tell me how to create a checkbox
using Xcode in iOS.
我是 Objective-C 的新手,所以我主要使用 Storyboard,有人请告诉我如何checkbox
在 iOS 中使用 Xcode创建一个。
采纳答案by AbdulRehman Warraich
CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.
CheckBox 在对象库中不可用。您可以为此目的使用第三方库,也可以自己创建它。
There is the working code for checkbox.
有复选框的工作代码。
create a class level variable and property of button in @inteface
在@inteface 中创建一个类级变量和按钮属性
@interface testViewController (){
BOOL checkBoxSelected;
}
@property (weak, nonatomic) IBOutlet UIButton *checkBox;
@end
in viewdidload set images for the button states.
在 viewdidload 中为按钮状态设置图像。
[_checkBox setBackgroundImage:[UIImage imageNamed:@"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:@"checkBoxChecked.png"]
forState:UIControlStateSelected];
and after create a button action in that button Action.
并在该按钮操作中创建按钮操作之后。
checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];
Hope it helps
希望能帮助到你
回答by NTP
UISwitch is the standard control used in IOS applications for making binary choices but if you want to use checkbox you can create a UIButton
UISwitch 是 IOS 应用程序中用于进行二进制选择的标准控件,但如果您想使用复选框,您可以创建一个 UIButton
@property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender
and change its background image on click event of UIButton
并在 UIButton 的单击事件上更改其背景图像
- (IBAction)CheckBoxClick:(id)sender {
if(!checked){
[_CheckBox setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if(checked){
[_CheckBox setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
also there are more detailed answers to this question at
这个问题也有更详细的答案
How to create a simple checkbox in iOS?
and if you dont want to use images you can check the following
如果您不想使用图像,您可以检查以下内容
回答by jrturton
UISwitch
is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?
UISwitch
是 iOS 中用于指示开/关、选中/未选中状态的标准控件。为什么不使用它?
回答by Bhadresh Mulsaniya
1) Create Prototype cell in UITableView.
1) 在 UITableView 中创建 Prototype 单元格。
2) Add one button inside the cell and set button style to Custom.
2) 在单元格内添加一个按钮并将按钮样式设置为自定义。
3) Set the image for checkbox.
3) 设置复选框的图像。
回答by Bhadresh Kathiriya
ios language doesn't use checkbox controller. but you are use checkbox that you are inport two image select & unselect
ios 语言不使用复选框控制器。但是您正在使用复选框,您要导入两个图像选择和取消选择
Step 1> Create button and set image.
步骤 1> 创建按钮并设置图像。
Step 2> Create button touch object method and put if condition for check & uncheck. for example:
步骤 2> 创建按钮触摸对象方法并放置检查和取消选中的条件。例如:
- (IBAction)btnLogin:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.tag == 0) {
btn.tag = 1;
//set image checked.
}
else{
btn.tag = 0;
//set image unchecked.
}
}
回答by Jagveer Singh
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
您不需要继承 UIButton 类。按照设计,Objective-C 更喜欢组合而不是继承。
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
UIButton 是 UIControl 的子类,它有一个 selected 属性。您可以使用此属性来切换复选框的开/关行为,就像 UISwitch 一样。
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:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}