Reputation: 453
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
Upvotes: 0
Views: 1835
Reputation: 453
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
Upvotes: 0
Reputation: 2983
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 :)
Upvotes: 3