简体   繁体   中英

Create Checkbox using Storyboard in Xcode

我是Objective-C的新手所以我主要使用Storyboard,有人请告诉我如何在iOS中使用Xcode创建一个checkbox

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

@property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender

and change its background image on click event of 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

Checkbox in iOS application

UISwitch is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?

CheckBox is not available in object library . You can use third party library for that purpose or you can create it by your self.

There is the working code for checkbox.

create a class level variable and property of button in @inteface

@interface testViewController (){
    BOOL checkBoxSelected;
}
@property (weak, nonatomic) IBOutlet UIButton *checkBox;

@end

in viewdidload set images for the button states.

 [_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

1) Create Prototype cell in UITableView.

2) Add one button inside the cell and set button style to Custom.

3) Set the image for checkbox.

ios language doesn't use checkbox controller. but you are use checkbox that you are inport two image select & unselect

Step 1> Create button and set image.

Step 2> Create button touch object method and put if condition for check & uncheck. for example:

- (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.
   }
 }

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

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM