Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a simple checkbox in iOS? [duplicate]

Possible Duplicate:
Checkbox in IPhone application

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

Thanks.

like image 810
Doom Avatar asked Mar 20 '11 11:03

Doom


People also ask

Does iOS have checkbox?

For example, iOS, iPadOS, macOS, and watchOS support the switch toggle style, whereas only macOS supports the checkbox style.

How do I create a checkbox in SwiftUI?

There is absolutely no need to create a custom View with a checkbox image or anything like that. Simply use the Toggle SwiftUI element with a styling set to CheckboxToggleStyle(). That is all you need to create native checkboxes in SwiftUi.


2 Answers

Yeah, no checkbox for you in 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!

like image 122
Gal Blank Avatar answered Sep 19 '22 13:09

Gal Blank


On iOS there is the switch UI component instead of a checkbox, look into the UISwitch class. 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.

like image 33
JustSid Avatar answered Sep 18 '22 13:09

JustSid