How to implement PickerView Programmatically in iPhone

This is the PickerView example. In this example we will see how to implement PickerView Programmatically in iPhone. So let see how it will worked. Another PickerView reference you can find out from here DatePicker SingleComponentPicker and DoublecomponentPicker

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “PickerView_Programatically”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add images in the resource folder.

Step 4: Open the PickerView_ProgramaticallyViewController.h file and make the following changes in the file.

#import <UIKit/UIKit.h>

@interface PickerView_ProgramaticallyViewController : UIViewController {

UIButton *pickerButton;
UIPickerView *myPickerView;
NSMutableArray *array_from;
UILabel *fromButton;
UIButton *doneButton ;
UIButton *backButton ;
}

@property(nonatomic,retain) IBOutlet UIButton *pickerButton;
@property(nonatomic,retain) IBOutlet UILabel *fromButton;

(IBAction)PickerView:(id)sender;

@end

Step 5: Double click the PickerView_ProgramaticallyViewController.xib file an open it to the Interface Builder. First drag the button and label, and place it to the view window. Select the button and bring up Attribute Inspector and give the Title “PickerView” and bring up Connection Inspector and connect File’s Owner icon to the button and select pickerButton. And connect Touch Up Inside to the File’s Owner icon select PickerView: method. Connect File’s Owner icon to the label and select fromButton. Now save the .xib file, close it and go back to the Xcode.

Step 6: In the PickerView_ProgramaticallyViewController.m file make the following changes:

#import "PickerView_ProgramaticallyViewController.h"

@implementation PickerView_ProgramaticallyViewController

@synthesize pickerButton,fromButton;

(IBAction)PickerView:(id)sender
{
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
[self.view addSubview:myPickerView];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;

doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
doneButton.frame = CGRectMake(265.0,202.0,  52.0, 30.0);
UIImage *img = [UIImage imageNamed:@"done.png"];
[doneButton setImage:img forState:UIControlStateNormal];

[img release];

[self.view addSubview:doneButton];
}

(IBAction)aMethod:(id)sender
{
[myPickerView removeFromSuperview];

[doneButton removeFromSuperview];

}

(void)dealloc
{
[super dealloc];
}

(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.
}

#pragma mark – View lifecycle

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

array_from=[[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iMac",@"Mac",
@"iBook",@"Safari",nil];

}

(void)viewDidUnload
{
[super viewDidUnload];
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// tell the picker how many rows are available for a given component
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 7;

return numRows;
}

// tell the picker how many components it will have
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[fromButton setText:[NSString stringWithFormat:@"%@",[array_from objectAtIndex:[pickerView selectedRowInComponent:0]]]];

}

(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

return [array_from objectAtIndex:row];

}

// tell the picker the width of each row for a given component
(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat componentWidth = 0.0;
componentWidth = 135.0;

return componentWidth;}

@end

Step 7: Now compile and run the application on the Simulator.

You can Download SourceCode from here PickerView_Programatically

Leave a Reply

Your email address will not be published. Required fields are marked *