UIButton ControlState not responding as wanted - iphone

Well, I have a custom uitabbarcontroller with 3 UIButtons on it (simulating tabs). I have set background images for normal, selected and highlighted states (selected and highlighted are both the same).
Everything works fine but one thing: When I have on tab (button) selected, and that tab gets pressed again, instead of highlighting it shows the button being pressed (getting darker). I have tried setting to NO the property adjustsImageWhenHighlighted, but instead of getting darker, it shows the Normal state background.
Any suggestion?
EDIT: This is the code I have in the UITabBarController subclass
#import "MyTabBarViewController.h"
#interface MyTabBarViewController ()
#end
#implementation MyTabBarViewController
ExploreViewController *exploreController;
ProfileViewController *profileController;
UIButton* leftButton;
UIButton* rightButton;
- (void)viewDidLoad
{
[super viewDidLoad];
exploreController = [[ExploreViewController alloc] initWithNibName:#"ExploreViewController" bundle:nil];
profileController = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:exploreController, profileController, nil];
[self addLeftButtonWithImage: [UIImage imageNamed:#"LeftTabBarIcon"] highlightImage:[UIImage imageNamed:#"LeftTabBarIcon_On"]];
[self addRightButtonWithImage: [UIImage imageNamed:#"RightTabBarIcon"] highlightImage:[UIImage imageNamed:#"RightTabBarIcon_On"]];
}
- (void) leftTabPressed
{
leftButton.selected = YES;
rightButton.selected = NO;
[self setSelectedViewController:exploreController];
}
- (void) rightTabPressed
{
rightButton.selected = YES;
leftButton.selected = NO;
[self setSelectedViewController:profileController];
}
-(void) addLeftButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.adjustsImageWhenHighlighted = NO;
[[leftButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[leftButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
leftButton.frame = CGRectMake(0.0, 367, 160.0, 49.0);
[leftButton addTarget:self action:#selector(leftTabSelectPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftButton];
}
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:#selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
}
#end

For your Comment your code will be like this:
-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.adjustsImageWhenHighlighted = NO;
rightButton.showsTouchWhenHighlighted = NO;
[[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted | UIControlStateSelected];
[rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
[rightButton addTarget:self action:#selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longGestureDetected:)]autorelease];
longGesture.delaysTouchesBegan = YES;
[rightButton addGestureRecognizer:longGesture];
[self.view addSubview:rightButton];
}
- (void)longGestureDetected:(UILongPressGestureRecognizer*)longGesture
{
if(longGesture.state == UIGestureRecognizerStateBegan)
[self rightTabPressed];
}

Trick seemed to be changing UIControlStateHighlighted to UIControlStateHighlighted | UIControlStateSelected. Seems odd, but it works.

Related

How to disable UIButton highlight action

I'm trying to implement an analog of segmented control with two buttons. In default state they have no images, only labels, in selected one they have background image. I want to activate control with TouchDown event.
here is the code (I removed all unnecessary things):
-(IBAction) onButton1
{
button1.selected = YES;
button2.selected = NO;
}
-(IBAction) onButton2
{
button1.selected = NO;
button2.selected = YES;
}
the problem is: assume button1 is selected. When I touch button2 it does not change its image to "selected" image (there is no default image, as I've said), but when I release finger it changes. Also, if I touch already selected button it removes "selected" image and return it when I release it.
I've set highlighted state of buttons, so they have "selected" image in that state, but it did not help (not only in IB, but also with [button setBackgroundImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateHighlighted];). I've set adjustsImageWhenHighlighted = NO, that did not help too, again, in both program way and IB.
I've seen a lot of similar (but not identical) questions here, but they did not work for me.
Thanks in advance
Since, you wanted to activate the action at touch down event, I assume that you dont need highlighting. Try the following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
[button1 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:#selector(button1TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button1];
button1.selected = YES;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(80.0f, 10.0f, 50.0f, 50.0f);
[button2 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(button2TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button2];
}
- (void)button1Action:(UIButton*)sender
{
button1.highlighted = NO;
button1.selected = YES;
button2.selected = NO;
}
- (void)button2Action:(UIButton*)sender
{
button2.highlighted = NO;
button2.selected = YES;
button1.selected = NO;
}
- (void)button1TouchUp:(id)sender
{
button1.highlighted = NO;
}
- (void)button2TouchUp:(id)sender
{
button2.highlighted = NO;
}

change backbutton with custom image and textcolor

I want to change the backbutton appearance for my backbutton in my navigation bar. For that I have this piece of code.
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = #"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
green:158/255.0
blue:218/255.0
alpha:1.0];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
But still I get the standard back button. Any help ?
Put your code to customize back button before pushing that viewController to the stack. And set BackBarButtonItem , not leftBarButtonItem.
UIImage *image = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[backbutton setImage:image forState:UIControlStateNormal];
backbutton.titleLabel.textColor = [UIColor blueColor];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[[self navigationItem] setBackBarButtonItem: back];
[self.navigationController pushViewController:yourViewControllerObject animated:YES];
using bellow code for your requirement..
- (void)viewDidLoad
{
///write your code
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
[backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backbutton setTitle:#"Back" forState:UIControlStateNormal];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
}
when back button clicked bellow method call and you will go to previous viewcontroller
-(void)back_Clicked{
[self.navigationController popViewControllerAnimated:YES];
}
Use
self.navigationItem.leftBarButtonItem = back;
Instead of
self.navigationController.navigationItem.leftBarButtonItem = back;
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[self buttonWithImage:#"backButton" heighlightImageName:#"backButton" buttonFrame:CGRectMake(2, 1, 30, 30) selectorName:#selector(buttonBackClicked:) target:self]]];
- (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempButton setFrame:buttonFrame];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
return tempButton;
}
- (UIImage*)getImageFromResource:(NSString*)imageName
{
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:#"png"]];
}
IButton *favButton = [[UIButton alloc] init];
[favButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:#selector(favouriteButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithCustomView:favButton];
self.navigationItem.rightBarButtonItem = button;
[button release];
[favButton release];

UIBarButtonItem init with custom view selector not working properly

I'm trying for hours to make a left button work properly and mimic a back button.
My code to create the button:
UIBarButtonItem *backButton = [self customBarButton:#"back_button" imageHiglighted:#"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = #selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Here the method called to create custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
And the action:
-(void)buttonPressed:(id) sender{
NSLog(#"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
If you could help me I'd be very grateful.
Thanks.
Do this add selector to custom button as it is view of bar buttom:
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
TRY THIS
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:#selector(btnBackClicked:) target:self]];
+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
[objButton setFrame:buttonFrame];
[objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
return objButton;
}
Try this..
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method..
I mean to say give selector to your button instead of bar button item..
Check and reply..
Just put this line in your code..
[backButton addTarget:self action:#selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
This worked for me
UIButton * tmpButton = [self generateButtonwithImageName:#"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;
-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=rect;
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
return button;
}
//working for me
if([version floatValue] < 7.0)
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.opaque = YES;
closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
[closeButton setImage:[UIImage imageNamed:#"btn_end.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"btn_end_on.png"] forState:UIControlStateHighlighted];
[closeButton addTarget:self action:#selector(actionClose) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{
/*add button back to navigation*//*add buttom back to navigation*/
UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:#"完了" style:UIBarButtonItemStylePlain target:self action:#selector(actionClose)];
self.navigationItem.rightBarButtonItem = itemBack;
}

How to fire hightlight effect on UIBarButtonItem

When you tap on a UIBarButtonItem in a UIToolbar, there is a white glow effect.
Is there a possibility to fire an event to show this effect?
I don't want to press the button. Only the effect should be displayed. I want to visualize to the user, that there is new content behind this button.
Thanks for your help!
Heres the "highlight.png", I'm not kidding! (Though you may not seeing it on a white background.)
The following method assumes you are using the toolbarItems property of a navigation controller and that the button you want to highlight is the last item in that array. You can apply it to any toolbar, of course, and pick a different array index if necessary.
Right now this is not exactly perfect, because the animation moves the new button from the lower left of the screen. However, I think that can be turned off with a little effort.
Note that I used PeakJi's image.
I hope this works for you.
Enjoy,
Damien
- (void)highlightButton {
NSArray *originalFooterItems = self.toolbarItems;
NSMutableArray *footerItems = [originalFooterItems mutableCopy];
[footerItems removeLastObject];
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:#"white_glow" ofType:#"png"];
UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile];
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
imageView.frame = CGRectMake(0, 0, 40, 40);
UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[footerItems addObject:flashImage];
[UIView animateWithDuration:0.3
delay: 0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
self.toolbarItems = footerItems; }
completion:^(BOOL finished){
[UIView animateWithDuration:.3
delay: 0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.toolbarItems = originalFooterItems;
}
completion:nil];
}];
}
For Bar items
[(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:#"highlight.png"] forState:UIControlStateNormal];
In General - Assuming you have a button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(someFunction:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Click here" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
[self.view addSubview:button];
you can at any given point, programmatically call this function:
[button setTitle:#"Look Here" forState:UIControlStateNormal];
or if you like to have an highlight image
btnImage = [UIImage imageNamed:#"highlight.png"];
[button setImage:btnImage forState:UIControlStateNormal];
A very simple alternative:
That said , you can also set the button like this:
- (void)highlightButton:(UIButton *)button {
[button setHighlighted:YES];
}
You can try to recreate a new bar button item with different style, and then reassign it back. My experiment was:
In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Custom"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightBtn;
[rightBtn release];
[self performSelector:#selector(highlight)
withObject:nil
afterDelay:2.0];
}
And in method highlight:
- (void) highlight{
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Custom"
style:UIBarButtonItemStyleDone
target:self
action:nil];
self.navigationItem.rightBarButtonItem = rightBtn;
[rightBtn release];
}
Your implementation might be different than mine, but as long as you are fine with reassigning the bar button, I think it'll work as expected. Good luck :)
If you have a category:
#define HIGHLIGHT_TIME 0.5f
#interface UIButton (Highlight)
- (void)highlight;
#end
#implementation UIButton (Highlight)
- (void)highlight {
UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
overlayButton.frame = self.frame;
overlayButton.showsTouchWhenHighlighted = YES;
[self.superview addSubview:overlayButton];
overlayButton.highlighted = YES;
[self performSelector:#selector(hideOverlayButton:) withObject:overlayButton afterDelay:HIGHLIGHT_TIME];
}
- (void)hideOverlayButton:(UIButton *)overlayButton {
overlayButton.highlighted = NO;
[overlayButton removeFromSuperview];
}
#end
Then you need code like this…
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
[self.navigationController.navigationBar.subviews.lastObject performSelector:#selector(highlight) withObject:nil afterDelay:2];
…to highlight the button in the navigation bar after 2 seconds.
A simple ay is to init your UIBarButtonItem with a CustomView (UIBUtton)
UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
[favButton setFrame:CGRectMake(0.0, 100.0, 50.0, 30.0)];
[favButton setBackgroundImage:[UIImage imageNamed:#"bouton_black_normal.png"]
forState:UIControlStateNormal];
[favButton setBackgroundImage:[UIImage imageNamed:#"bouton_black_hightlight.png"]
forState:UIControlStateHighlighted];
[favButton setTitle:#"Add" forState:UIControlStateNormal];
[favButton setTitle:#"Add" forState:UIControlStateHighlighted];
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[[favButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]];
[favButton addTarget:self action:#selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:favButton];

change image on click of button in objective c

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 change from first.png to second.png..
and when user selects on btn2, image of btn2 should change from first.png to second.png, and also change image of btn1 to default again first.png, so that user came to know he has clicked 2nd button.
how should i do this ?
Thanks In Advance !!
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];
}
#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;
}
}
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];
}
}