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.


Friday 17 June 2011

Accelerometer in iPhone



1) Start a new project in Xcode
2) Add file new file MainViewController.h and MainViewController.m with MainViewController.xib, by ticking the xib checkbox while adding.
3) Below is the xib file to be designed and make connections accordingly.




MainViewController.h

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController <UIAccelerometerDelegate> {
  IBOutlet UILabel *labelX;
  IBOutlet UILabel *labelY;
  IBOutlet UILabel *labelZ;
  
  IBOutlet UIProgressView *progressX;
  IBOutlet UIProgressView *progressY;
  IBOutlet UIProgressView *progressZ;
  
  UIAccelerometer *accelerometer;
}

@property (nonatomic, retain) IBOutlet UILabel *labelX;
@property (nonatomic, retain) IBOutlet UILabel *labelY;
@property (nonatomic, retain) IBOutlet UILabel *labelZ;

@property (nonatomic, retain) IBOutlet UIProgressView *progressX;
@property (nonatomic, retain) IBOutlet UIProgressView *progressY;
@property (nonatomic, retain) IBOutlet UIProgressView *progressZ;

@property (nonatomic, retain) UIAccelerometer *accelerometer;

@end


MainViewController.m

#import "MainViewController.h"


@implementation MainViewController

@synthesize labelX;
@synthesize labelY;
@synthesize labelZ;

@synthesize progressX;
@synthesize progressY;
@synthesize progressZ;

@synthesize accelerometer;

/*
 // 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];
  
  self.accelerometer = [UIAccelerometer sharedAccelerometer];
  self.accelerometer.updateInterval = .1;
  self.accelerometer.delegate = self;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
  labelX.text = [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x];
  labelY.text = [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y];
  labelZ.text = [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z];
  
  self.progressX.progress = ABS(acceleration.x);
  self.progressY.progress = ABS(acceleration.y);
  self.progressZ.progress = ABS(acceleration.z);
}


/*
// 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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end


4) In .m file above the "didAccelerate" is an AccelerometerDelegate method.
5) Build and Run on device.
NOTE : Accelerometer does not works on Simulator.