Checkbox control for iOS application - ios

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.

You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}

//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}

This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Related

CheckBox issues with images

When I select the check box the second time it shows me the checked image, but the status is unchecked.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateSelected];
}
}
Your code is totally right just you replace UIControlStateSelected to UIControlStateNormal.
You need add normal image and selected images in IB itself (or in viewDidLoad) and toggle state for button in button action like below
- (IBAction)checkBoxAction:(UIButton *)sender {
sender.selected = !sender.selected;
}
if (sender.tag==0)
{
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
else
{
sender.tag=0;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
When create button for check box you can save address this element on array after click on every button first access to all object on array seved and set image unckeck and this button clicked set image checked.
Or
When create button for check box set tag different for every button and when click on button you should be set image unckeck for every button use tag number and set image check for button clicked.
Also one thing,
Here you use CheckBox.selected = YES/NO;
So also you can write your code like:
- (void)viewDidLoad {
[super viewDidLoad];
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateSelected];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
}
else
{
CheckBox.selected = NO;
}
}
- (IBAction)changeState:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.selected == NO)
{
btn.selected = YES;
[btn setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
btn.selected = NO;
[btn setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
}
You should set boolean value to false in viewDidLoad and then in button click
- (IBAction)btnCheckBoxClick:(id)sender
{
if (isChecked)
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox.png"] forState:UIControlStateNormal];
isChecked = false;
}
else
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox_selected.png"] forState:UIControlStateNormal];
isChecked = true;
}
}

Set only single option in iOS

I have two custom round rect button and on this did tap event on image show.Like When we select gender there is only one option to select other automatically hide.but my gives when i touch first button than it select but when i touch second button no operation select.
My main moto is that i want to choose gender but in iOS no radio button use so i design custom and on click event set image.
calling api is a string type.
-(void)viewdidload
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
callApi=#"MALE";//calling Api is a string type//
[button addTarget:self action:#selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)roundButtonDidTap:(UIButton*)tappedButton
{
if([callApi isEqualToString:#"FEMALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button setImage:btnImage forState:UIControlStateNormal];
}
if ([callApi isEqualToString:#"MALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button2 setImage:btnImage forState:UIControlStateSelected];
button.imageView.hidden=YES;
}
}
try this code:
-(IBAction)selectMale:(id) sender{ // MALE BUTTON
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Male";
[btnFemale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnFemale setSelected:NO];
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}
}
-(IBAction)selectFemale:(id) sender{
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonActionFemale:RadioButton1];
}
-(void)radiobuttonActionFemale:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Female";
[btnMale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnMale setSelected:NO];
}
else
{

how to check three button conditions

I am having three button image, I want to check the conditions between them. when I click the button 1 means background image change to blue colour. at the same time I click the button 1 it will move to normal state white colour. Same for another two buttons.For that process i am using this code. [tempBtn setSelected:! tempBtn.selected];
ButtonImageSelected = [UIImage imageNamed:#"lblue.png"];
ButtonImage = [UIImage imageNamed:#"l.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setBackgroundImage:ButtonImage forState:UIControlStateNormal];
button1.tag = 1;
[button1 setBackgroundImage:ButtonImageSelected
forState:UIControlStateSelected];
button1.userInteractionEnabled = YES;
[button1 addTarget:self
action:#selector(button1Selector:)
forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button1];
Another two buttons are same as button 1.
My condition is: when button 1 is clicked means button 3 should not change. when button 3 is clicked means button 1 should not change.button 2 can select in both the condition.I am using below code to check the conditions but its not working correct.
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
}
help me to how to check the conditions.In the above method.
I believe what you are trying to accomplish is to have each button change states as you press them. Once you have defined your buttons, this is how you can ensure they alternate images for each state.
Alternating Images Based on Button Presses
-(IBAction)buttonPressed:(id)sender {
switch ([sender tag]) {
case 1:
if ([UIImagePNGRepresentation(button1.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default1. png"])]) {
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateNormal];
} else {
[button1 setImage:[UIImage imageNamed:#"default1.png"] forState:UIControlStateNormal];
}
break;
case 2:
if ([UIImagePNGRepresentation(button2.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default2. png"])]) {
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateNormal];
} else {
[button2 setImage:[UIImage imageNamed:#"default2.png"] forState:UIControlStateNormal];
}
break;
case 3:
if ([UIImagePNGRepresentation(button3.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default3.png"])]) {
[button3 setImage:[UIImage imageNamed:#"selected3.png"] forState:UIControlStateNormal];
} else {
[button3 setImage:[UIImage imageNamed:#"default3.png"] forState:UIControlStateNormal];
}
break;
default:
break;
}
}
Note: make sure you add the action to each button, and change the names of the images
Adding the Actions to Each Button
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
Ensure you have done the following:
Added action to each button
Tagged each button accordingly
Set the initial images of each button to match the condition
Try this. Before that make sure the tags are proper for every button
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
else{
button1.userInteractionEnabled = YES;
button2.userInteractionEnabled = YES;
[tempBtn setSelected:!tempBtn.selected];
}
}

I want to change the UIButton Highlight Image, but without success

I have a UIbutton in my view
I want to change the UIButton Highlight Image, but without success
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 66, 29)];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- (void)filterAction:(UIButton *)button
{
if(button.isSelected == YES)
{
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
button.selected = NO;
}
else
{
#warning I want to change the UIButton Highlight Image, but without success
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_normal.png"] forState:UIControlStateHighlighted];
button.selected = YES;
}
}
If you want your button to behave as follows,
normal - filter_button_normal
highlighted (on touch press down on button) - filter_button__down_selected
selected (after removing touch from button) - filter_button_selected;
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateHighlighted];
so when the user touches it , image changes to highlighted and when he removes his finger over it, it changes to selected
And the target you added is UIControlEventTouchUpInside
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
so filterAction is called after the user removes his finger from it.
- (void)filterAction:(UIButton *)button
{
// the image will change automatically
if(button.isSelected == YES)
{
button.selected = NO;
}
else
{
button.selected = YES;
}
}

How to change button image added to TableView section header

I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}

Resources