3

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.

6 Answers6

3

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

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • what if i have to use agree condition and before that i have to use check terms and conditions. –  Jun 14 '16 at 11:21
3

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

Community
  • 1
  • 1
NTP
  • 4,338
  • 3
  • 16
  • 24
1

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

Abdul Rehman
  • 2,386
  • 1
  • 20
  • 34
0

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.

Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

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.
   }
 }
Bhadresh Kathiriya
  • 3,147
  • 2
  • 21
  • 41
0

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
}
Jagveer Singh
  • 2,258
  • 19
  • 34