i draged 3 button in my .xib file (btn1,btn2,btn3 respectively) and initially i given default image to them, first.png
now when user clicks on btn1, image of btn1 should
#import <Foundation/Foundation.h>
@interface CustomRadioButton : UIButton {
}
@end
#import "CustomRadioButton.h"
@implementation CustomRadioButton
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Set the refresh icon as the button's image
[self setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateSelected];
// When the button is pressed, draw to button with less opacity.
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
@end
inside the Implement file of the viewController
- (void)viewDidLoad {
[super viewDidLoad];
for (int i=1;i<=2;i++){
CustomRadioButton *Radiobutton = [CustomRadioButton buttonWithType:UIButtonTypeCustom];
Radiobutton = [[CustomRadioButton alloc] initWithFrame:CGRectMake(200,50*i, 30, 30)];
[Radiobutton addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
Radiobutton.tag=i;
[self.view addSubview:Radiobutton];
}
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
You have to set button action method code
-(IBAction)btnClked:(id)sender
{
[btn1 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
[btn3 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
}
Set the images in your button call method like:
BOOL first;
-(void)firstBtnPressed
{
if(first == YES){
[btn1 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
first = NO;
}
else
{
first = YES;
[btn1 setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
}
}