Search Posts in my Blog

Tuesday, 21 June 2011

Customized Radio Button in iPhone

1) Create a new project in Xcode
2) Add new file SelectRadioButton.h and SelectRadioButton.m  with SelectRadioButton.xib file.

SelectRadioButton.xib





SelectRadioButton.h


#import <UIKit/UIKit.h>


@interface SelectRadioButton : UIViewController {

IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UILabel *lbl3;
IBOutlet UILabel *lbl_Selected;
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
IBOutlet UIButton *btn3;
}

-(IBAction)btn1Clicked;
-(IBAction)btn2Clicked;
-(IBAction)btn3Clicked;

@property (nonatomic,retain) IBOutlet UILabel *lbl1;
@property (nonatomic,retain) IBOutlet UILabel *lbl2;
@property (nonatomic,retain) IBOutlet UILabel *lbl3;
@property (nonatomic,retain) IBOutlet UILabel *lbl_Selected;

@property (nonatomic,retain) IBOutlet UIButton *btn1;
@property (nonatomic,retain) IBOutlet UIButton *btn2;
@property (nonatomic,retain) IBOutlet UIButton *btn3;

@end


3) Make all connection in xib fine accordingly
4) radioButtonDisabled.png and radioButtonActiveHover.png are two png's included in the project.
 Open and save the images. Drag these images to your project.







SelectRadioButton.m

#import "SelectRadioButton.h"


@implementation SelectRadioButton

@synthesize lbl1;
@synthesize lbl2;
@synthesize lbl3;
@synthesize lbl_Selected;
@synthesize btn1;
@synthesize btn2;
@synthesize btn3;




/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];
}

-(IBAction)btn1Clicked
{
[btn1 setSelected:YES];

[btn1 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn2 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn3 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl1.text;

}


-(IBAction)btn2Clicked
{
[btn2 setSelected:YES];
[btn2 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn3 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl2.text;
}


-(IBAction)btn3Clicked
{
[btn3 setSelected:YES];
[btn3 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn2 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl3.text;
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
lbl1=nil;
lbl2=nil;
lbl3=nil;
lbl_Selected=nil;
btn1=nil;
btn2=nil;
btn3=nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
[lbl1 release];
[lbl2 release];
[lbl3 release];
[lbl_Selected release];
[btn1 release];
[btn2 release];
[btn3 release];
    [super dealloc];
}


@end


OUTPUT:



NOW Build and Run n' enjoy the new creation :)

Download the Source Code HERE.


4 comments:

  1. its really gr8 tutorial , i have done with radio buttons which i wish 2 do
    ,thanks a lot & keep it up :)

    ReplyDelete
    Replies
    1. Thanks Yogesh... Good to know that it helped you :) :)

      Delete
  2. Its really good to the Beginners. Thanks Rahul :)

    ReplyDelete
    Replies
    1. YOu are welcome Vanitha .. Good to know it helped you. :)
      iCoding Rocks \m/ :)

      Delete