This is the “PickerApp” example. There are many ways to display the “PickerApp” in the iPhone. I am going to show you the simplest way to execute the Picker View.
Step 1: Open the Xcode and create a new Xcode project using View base application template. Give the application name “PickerApp”. As shown in the figure below:
Step 2: Expand classes and notice Interface Builder created the PickerAppViewController.h and pickerAppViewController.m class for you. Expand Resources and notice the template generated a separate nib, PickerAppViewController.xib, for the PickerApp.
Step 3: Open the PickerAppViewController.h file and make the following changes in the file.
@interface PickerAppViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UILabel *label;
NSMutableArray *arrayC;
IBOutlet UIPickerView *pickerView;
}
@property (nonatomic, retain) UILabel *label;
@end
Step 4: Double click the PickerAppViewController.xib file and after that make the following changes.
A) Open the view window, first drag the picker view from the library and place it to the view window and select the label and bring up Attribute Inspector and delete the text.
B) Connect File’s Owner icon to label and select “View”.
Once this is done, save the PickerAppViewController.xib file, close it and go back to the Xcode.
Step 5: Open the PickerAppViewController.m file and make the following changes in the file.
@implementation PickerAppViewController
@synthesize label;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
– (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad {
[super viewDidLoad];
arrayC = [[NSMutableArray alloc] init];
[arrayC addObject:@"Red"];
[arrayC addObject:@"Green"];
[arrayC addObject:@"Blue"];
[arrayC addObject:@"Yello"];
[arrayC addObject:@"Pink"];
[arrayC addObject:@"White"];
[pickerView selectRow:1 inComponent:0 animated:NO];
label.text= [arrayC objectAtIndex:[pickerView selectedRowInComponent:0]];
}
– (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
– (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text= [arrayC objectAtIndex:row];
}
– (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayC count];
}
– (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arrayC objectAtIndex:row];
}
/*
// 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
Step 6: Now build and run the code and view the Output in the Simulator.
You can download source code from here PickerApp