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;
}
}
Related
I have taken two custom buttons and one method, to set their check and uncheck
In viewdidload() i have written like this..
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO; //declared as boolean to change check and uncheck for button one
isChecked_02 = NO; //declared as boolean to change check and uncheck for button two
And in method
-(IBAction)checkboxOnClick:(UIButton *)sender {
if (sender.tag == 1) {
if (isChecked_01 == NO) {
[checkbox_01 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
isChecked_01 = YES;
}
else
{
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO;
}
}
if (sender.tag == 2) {
if (isChecked_02 == NO) {
[checkbox_02 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
isChecked_02 = YES;
}
else
{
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_02 = NO;
}
}
}
is there any other way to create check boxes in ios..? i have taken each checkbox with one isChecked boolean value for it.. is there posibilities to use only one boolean for entire check list.....
You can use selected property (inherited from UIControl) instead of your isChecked_01 and isChecked_02:
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_01 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[checkbox_02 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
- (IBAction)checkboxOnClick:(UIButton *)sender {
sender.selected = !sender.selected;
}
Check this sample code, might be useful to if you want to do customization with checkboxes
https://github.com/Marxon13/M13Checkbox
In Storyboard
Set default checkbox image as like below.
Change the State Config to Selected and add image like checkbox enabled
In CODE
Objective C
- (IBAction)btnCheckTerms:(UIButton *)sender {
_btnCheckboxTerms.selected = !_btnCheckboxTerms.selected; }
SWIFT
#IBAction func btnCheckTerms(sender : UIButton) {
_btnCheckboxTerms.isSelected = !_btnCheckboxTerms.isSelected; }
Extra Tips
Use material Icons rather than normal icon images.
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 use button is in either way
i want to use if user select one time button then its display different color after that click on button it appear as usual after click it display that color
static int tapCount = 0;
- (IBAction)BtnClickdemo:(id)sender
{
tapCount++;
if (tapCount == 1)
[btnClick setImage:[UIImage imageNamed:#"line.png"] forState:UIControlStateNormal];
else if (tapCount == 0)
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
else
{
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
}
}
User only select 2 tappded like Ratingbar taped ??
How can i do ??
You can assign a BOOL value to your button and check this value everytime the button is tapped:
#implementation youCustomViewController {
BOOL buttonSelectionned;
UIButton *myButton;
}
- (void)viewDidLoad
{
/* Init objects */
buttonSelectionned = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100);
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
and then implement your method buttonTapped
- (void)buttonTapped:(id)sender
{
if (buttonSelectionned) {
buttonSelectionned = NO;
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
} else {
buttonSelectionned = YES;
myButton setImage:[UIImage imageNamed:#"MinSelected2.png"] forState:UIControlStateNormal];
}
}
I have a UIButton that has no title or initial background image. When pressed, the button background image changes and when pressed again, it is set to nil. Now with iOS 6.x, the button completely disappears. Here is my code:
- (IBAction)resetAll: (id) sender {
[self reset];
for (UIView *view in self.view.subviews) {
if ([[[view class] description] isEqualToString:#"UIRoundedRectButton"]) {
UIButton *theButton = (UIButton *)view;
int nTag = theButton.tag;
switch (nTag) {
case 11: {
theButton.tag = 10;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
break;
}
case 21: {
theButton.tag = 20;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 31: {
theButton.tag = 30;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 41: {
theButton.tag = 40;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 51: {
theButton.tag = 50;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
}
}
}
return;
This code works fine:
- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;
// Get the current tag value
int currentTag = [sender tag];
// Toggle the check buttons
if (currentTag == 10 || currentTag == 20 || currentTag == 30 || currentTag == 40 || currentTag == 50) {
[btn setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
btn.tag = ++currentTag;
}
else
if (currentTag == 11 || currentTag == 21 || currentTag == 31 || currentTag == 41 || currentTag == 51) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
btn.tag = --currentTag;
}
[self calculate];
Any suggestions or help would be appreciated!
Instead of changing the background image of the button in the UIControlStateNormal, why not instead just change states of the button?
If you created the button programmatically, you just need to add the line
[btn setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
Then, when pressed and you want it to be the arrow image:
[btn setSelected:YES];
and to set it back to the default appearance:
[btn setSelected:NO];
If you made the button in a XIB, you can set the state images by changing the state config to Selected and set the image there.
Here is the final code implementing DOC's answer. I set the default and selected state in interface builder:
- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;
if ([btn isSelected ]) {
[btn setSelected:NO];
} else {
[btn setSelected:YES];
}
[self calculate];
}
- (IBAction)resetAll: (id) sender {
[self reset];
for (UIView *view in self.view.subviews) {
if ([[[view class] description] isEqualToString:#"UIRoundedRectButton"]) {
UIButton *theButton = (UIButton *)view;
[theButton setSelected:NO];
}
}
Just an additional answer in case (like me) there is already a good amount of logic around the selected state of the button (parent class gives state to the view based on this). If you need the button to start in an un-selected state with an image and change state to selected with the no image, then you can add a generated blank image to the selected state like so.
//Make a stupid blank image because setting to nil on a state doesn't work.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(23, 23), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *buttonNonSelectedImage = [UIImage imageNamed:#"video-slomo"];
[self.myButton setBackgroundImage:buttonNonSelectedImage forState:UIControlStateNormal];
[self.mybutton setBackgroundImage:blank forState:UIControlStateSelected];
[self.myButton addTarget:self
action:#selector(onMyButtonPress)
forControlEvents:UIControlEventTouchUpInside];
self.myButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.playRateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self.view addSubview:self.myButton];
Then, in your button action you can just change the state (and in my case add text).
- (void)onMyButtonPress
{
if (self.myButton.isSelected) {
[self.myButton setSelected:NO];
}
else {
[self.myButton setTitle:#"Selected"]
forState:UIControlStateSelected];
[self.myButton setSelected:YES];
}
}
The accepted answer works in most cases, I just needed the selected state for other tracking info. Happy programming.
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];
}
}