Toggle the button Image - ios

After i implemented a code for give an check image for a button but i want to give check image for no of buttons.
note: if i click on one button check image can be displayed and the same time i click on another button check image displayed on that particular button and previous button comes normal position.
i implement the code for single button here like this.
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"btn_check_on.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"bread_Wheat_rectangle.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
The above code is executed successfully but how to use this code for no of buttons.
please suggest any tutorial regarding my problem

This is how I have implemented it for one button. You can use it for more buttons too.
-(IBAction)ButtonAction
{
if (Flag==0)
{
Flag=1;
[myButton setImage:[UIImage imageNamed:#"checkbox-filled.png"] forState:UIControlStateNormal];
}
else
{
[myButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
Flag=0;
}
}
Note : If you want only one button to get checked Just set all other buttons image as checkbox.png and the selected one's checkbox-filled.png.
EDIT
You can make a class for checkbox and then use it. Here is the code...
CheckButton.h
#import <Foundation/Foundation.h>
#interface CheckButton : UIButton {
BOOL _checked;
int chkButtonClickVal;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
-(int)chkButtonClickVal;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
chkButtonClickVal=0;
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) dealloc
{
chkButtonClickVal=0;
[super dealloc];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checkbox-checked.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=1;
}
else
{
UIImage* img = [UIImage imageNamed:#"checkbox.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=2;
}
//NSLog(#"%d",chkButtonClickVal);
}
-(int)chkButtonClickVal
{
return chkButtonClickVal;
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
I have done it in same way. Try you'll be able to achieve it.
Good Luck :)

After long practicing this problem we can use switch cases it can be done very easily
switch (currenttagvalue) {
case 1:
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"] forState:UIControlStateNormal];
[level2 setImage:[UIImage imageNamed:#"ic_launcher.png"] forState:UIControlStateNormal];
[level3 setImage:[UIImage imageNamed:#"bread_sourdough_rectangle.png"] forState:UIControlStateNormal];
}
in this level is "IBOutlet UIButton level1;"
And then i implement so more buttons

Related

When clicked on button the image is not changing for the first time. Button image is to be changed when clicked for the first time

if( [[favbutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"wishlwhite.png"]])
{
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
You really shouldn't be checking the image in the button to determine state. Declare a separate property to test, e.g.
#property (nonatomic, assign) BOOL isWishList; // or something
-(IBAction) toggleButton: (id) sender {
if (self.isWishList) {
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
} else {
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
self.isWishList = !self.isWishList
}

How to make image on unbutton persist until its not changed by the user .?

I am working on UIButton as a check box, when I press button than checkbox selected image is appear and when press again checkbox unselected image is appear. if I press button image is appear, but it again disappear when I come back to that view again.
- (void)viewDidLoad {
[super viewDidLoad];
_car.delegate = self;
_car.keyboardType = UIKeyboardTypeNumberPad;
[_PreCheck setBackgroundImage:[UIImage imageNamed:#"checkbx_unchecked#2x.png"] forState:UIControlStateNormal];
[_PreCheck setBackgroundImage:[UIImage imageNamed:#"checkbx#2x.png"] forState:UIControlStateSelected];
[_PreCheck addTarget:self action:#selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)checkboxSelected:(id)sender
{
if ((checkBoxSelected = !checkBoxSelected)) {
[_PreCheck setSelected:checkBoxSelected];
NSLog(#"selected");
NSString *selected = #"selected";
NSUserDefaults *select = [NSUserDefaults standardUserDefaults];
[select setObject:selected forKey:#"checkboxselected"];
[select synchronize];
}
else
{
NSLog(#"not selected");
[_PreCheck setSelected:notselected];
}
}
Based on your posted code your ViewDidLoad must be check with current NSUserDefaults store value is selected or not based on it first you need to set button check uncheck image. so following is your viewDidLoad Code look like:
- (void)viewDidLoad {
[super viewDidLoad];
_car.delegate = self;
_car.keyboardType = UIKeyboardTypeNumberPad;
NSString *state = [[NSUserDefaults standardUserDefaults] stringForKey:#"btnstate"];
if([state isEqualToString:#"selected"])
{
[_PreCheck setBackgroundImage:[UIImage imageNamed:#"checkbx#2x.png"] forState:UIControlStateNormal];
[_PreCheck setSelected:YES];
}
else
{
[_PreCheck setBackgroundImage:[UIImage imageNamed:"checkbx_unchecked#2x.png"] forState:UIControlStateNormal];
[_PreCheck setSelected:NO];
}
[_PreCheck addTarget:self action:#selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside];
}
Now set the method for check-uncheck like following:
-(void)checkboxSelected:(UIButton*)sender
{
sender.selected = !sender.selected;
if([sender isSelected])
{
[_PreCheck setBackgroundImage:[UIImage imageNamed:#"checkbx#2x.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:#"selected" forKey:#"btnstate"];
}
else
{
[_PreCheck setBackgroundImage:[UIImage im ageNamed:#"checkbx_unchecked#2x.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:#"notselected" forKey:#"btnstate"];
}
}
Hope that work or you. just replace your code with my posted code no need to do other changes.
Store the button state in NSUSerDefaults.
Then In viewDidLoad get the state and based on that load image.
- (void)viewDidLoad
{
// Your code
// get selected state from user defaults.
BOOL selected = [[NSUserDefaults standardUserDefaults] boolForKey:#"buttonstateKey"];
if (selected)
{
// set selected image
}
else
{
// set regular image
}
}

iOS: How to check what the button background image is?

I am trying the following method ,but it isn't work. Is there a way I can check what my current background image is for my UIButton and compare it?
UIImage *buttonCheckImage = checkButton.currentBackgroundImage;
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}
this works using
[ UIImagePNGRepresentation( uiImage1 ) isEqualToData:
UIImagePNGRepresentation( uiImage2 ) ];
UIImage *buttonCheckImage = [self.btn_Checkbox imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkbox"];
if([UIImagePNGRepresentation(buttonCheckImage) isEqualToData:UIImagePNGRepresentation(checkImage)])
{
self.btnImage = [UIImage imageNamed:#"checkbox1"];
}
else
{
self.btnImage = [UIImage imageNamed:#"checkbox"];
}
[self.btn_Checkbox setImage:self.btnImage forState:UIControlStateNormal];
using : http://stackoverflow.com/questions/14078283/comparing-two-images-whether-same-or-not-ios
Hope its working well try this....
.h
#property (weak, nonatomic) IBOutlet UIButton *checkButtonObj;
-(IBAction)buttonClick:(id)sender;
.m
-(void)viewDidLoad {
[super viewDidLoad];
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
-(IBAction)buttonClick:(id)sender {
if ([self.checkButtonObj.imageView.image isEqual:[UIImage imageNamed:#"correct.png"]])
[self.checkButtonObj setImage:[UIImage imageNamed:#"wrong.png"] forState:UIControlStateNormal];
else
[self.checkButtonObj setImage:[UIImage imageNamed:#"correct.png"] forState:UIControlStateNormal];
}
Try the following code,
UIImage *buttonCheckImage = [checkButton imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:#"checkBox.png"];
if (buttonCheckImage == checkImage) {
}

How to create a segue to another view from a button in iCarousel

I'm using an iCarousel where i display some buttons. But how do I get to another view when the button is tapped?
I have this code already:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return 10;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//Action
}
How do I get to another view when the button is tapped? Can I use the prepareForSegue method here as well?
Thanks for your help!!
Sure, you just have to drag and create a segue from the first viewController to the second, and name it "segue0" for instance.
-(void)buttonTapped:(UIButton*)sender
{
NSInteger *index = [carousel indexOfItemViewOrSubview:sender];
if(index = 0)
{
[self performSegueWithIdentifier:#"segue0", sender:self];
}
//else if etc.., or better yet, ditch the if, do the checking in 'prepare' as explained below
}
-(void)prepareForSegue:(UIStoryboardSegue*) segue sender:(id)sender
{
YourNextViewController *nextView = [segue destinationViewController];
//Do stuff to your next view with "nextView"
}
If they are all going to the same viewController, but by sending different data in prepareForSegue, then you can ditch the if-statement, and send sender instead of self as the sender in [self performSegue..], and check in prepareForSegue.. which button is the sender(or moving your NSInteger *index... line down here)

UIButton's imageView property and hidden/alpha value

I have a UIButton that should display an activity indicator instead of an image in some situations. What I do at the moment is setting the hidden property of the button's imageView to YES and back. I also tried this with setting the alpha value to 0.0f and back to 1.0f.
This works until the state of the button changes. This resets the properties of the imageView and leads to hidden == NO and alpha == 1.0f.
Has anybody did something similar or has an idea how to hide the imageView of a button while the rest of it stays visible?
You can achieve this by playing with the transform property of view's layer i.e
To hide
swift code
button.imageView?.layer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
objective-C code
button.imageView.layer.transform = CATransform3DMakeScale(0, 0, 0);
To unhide
button.imageView?.layer.transform = CATransform3DIdentity
objective-C code
button.imageView.layer.transform = CATransform3DIdentity
It is simple.
Button have different states. Just set zero image for selected state and your image for normal state. When you want to display activity indicator set button state as selected and add indicator as button subview.
- (void)initializeButton {
button = [[UIButton alloc] init];
[button addTarget:self action:#selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[[UIImage alloc] init] forState:UIControlStateSelected]; //zero image
[button setImage:yourImage forState:UIControlStateSelected]; //your image
}
- (void)buttonDidClick:(UIButton *)button {
button.selected = YES;
/* Add yor activity indicator with some view tag or save reference to it */
}
Call next method when your activity is finished
- (void)finishActivityWithButton:(UIButton *)button {
button.selected = NO;
/* Remove your activity indicator from button */
}
set the tint color to [UIColor clearColor] if you are using a template image (should maybe work on others too, didn't try)
[[button imageView] setTintColor:[UIColor clearColor]];
You can achieve this by removing the image from the button when you want to hide it and assigning the image back to button when you need to show the image.
When you want to hide the image write like:
[yourButton setImage:nil forState:UIControlStateNormal];
and when you want to show the image back:
[yourButton setImage:yourImage forState:UIControlStateNormal];
Here yourButton is your UIButton and yourImage is a UIImage which holds the button image.
change the transform or select state will cause another problem, the best way is using category:
const char kNormalImage;
const char kSelectImage;
const char kDisableImage;
#interface UIButton ()
#property (nonatomic, strong) UIImage *normalImage;
#property (nonatomic, strong) UIImage *selectImage;
#property (nonatomic, strong) UIImage *disableImage;
#end
#implementation UIButton (Hidden)
- (void)setImageViewHidden:(BOOL)hidden {
if (hidden) {
self.normalImage = [self imageForState:UIControlStateNormal];
self.selectImage = [self imageForState:UIControlStateSelected];
self.disableImage = [self imageForState:UIControlStateDisabled];
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setImage:blank forState:UIControlStateNormal];
[self setImage:blank forState:UIControlStateSelected];
[self setImage:blank forState:UIControlStateDisabled];
} else {
[self setImage:self.normalImage forState:UIControlStateNormal];
[self setImage:self.selectImage forState:UIControlStateSelected];
[self setImage:self.disableImage forState:UIControlStateDisabled];
}
}
- (UIImage *)normalImage {
return objc_getAssociatedObject(self, &kNormalImage);
}
- (void)setNormalImage:(UIImage *)normalImage {
objc_setAssociatedObject(self, &kNormalImage, normalImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)selectImage {
return objc_getAssociatedObject(self, &kSelectImage);
}
- (void)setSelectImage:(UIImage *)selectImage {
objc_setAssociatedObject(self, &kSelectImage, selectImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)disableImage {
return objc_getAssociatedObject(self, &kDisableImage);
}
- (void)setDisableImage:(UIImage *)disableImage {
objc_setAssociatedObject(self, &kDisableImage, disableImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end

Resources