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 <uipickerViewDelegate> {

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 Comment:

8 comments
Lawrence says June 28, 2011

I am wondering if you can help me out, I have been trying this code, and following the tutorial, but my build fails everytime. In the “PickerView_ProgrmaticallyViewController.h” I keep getting a problem with code right here

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

the “myPickerView.delegate = self;” area keeps coming up with the code problem. I am very new to this, any suggestions? Thank you very much and great tutorials.

Reply
    Sushant says June 28, 2011

    In the PickerView_ProgrmaticallyViewController.h file make the following changes.

    @interface PickerView_ProgramaticallyViewController : UIViewController {

    Hope this will help you.

    Reply
Lawrence says June 28, 2011

yaaaaaaaaa got it haha sorry again for all the msg, and love both the support and the tutorials! Great work.

Reply
Kevin says June 27, 2012

Hello, I want to use the output of the picker for a comparison in an if statement to call a function. Should I use fromButton or pickerView? What would you recommend?

here is the if statement

if (fromButton == @”iPad”) {
[self playEvent3];
}

Reply
Kevin says June 27, 2012

Hello, I want to use the output of the picker for a comparison in an if statement to call a function. Should I use fromButton or pickerView? What would you recommend?

here is the if statement

if (fromButton == @"iPad") {
[self playEvent3];
}

Reply
Kevin says June 27, 2012

Hello, I want to use the output of the picker for a comparison in an if statement to call a function. Should I use fromButton or pickerView? What would you recommend?

here is the if statement

if (fromButton == @\&quot;iPad\&quot;) {
[self playEvent3];
}

Reply
Sou says September 10, 2012

i did the same coding as you hav given but in the result i can’t scroll the values and display it in the label …. pl help

Reply
Swati says November 6, 2012

Hi,
I want the picker view to be generated when i will select a tableviewcell.
I am new to ios programming.

Reply
Add Your Reply