1. Add a New File, subclass of ViewController, to your project. In my example, the name of my class is ViewController.
2. Open ViewController.xib and drag a pickerview to your xib file.
@property (nonatomic, strong) IBOutlet UIPickerView *myPickerView; //.h
@synthesize myPickerView; //.m
4. Connect your IBOutlet property to the object in your .xib file.
5. Go back to your ViewController.h, and make your interface header look like this:
@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
6. On your viewDidLoad, set the delegate and datasource of your pickerview to self;
- (void)viewDidLoad
{
[super viewDidLoad];
myPickerView.delegate = self;
myPickerView.dataSource = self;
}
7. Add an array that will hold your items for you.
If your items are constant, use NSArray, else, use NSMutableArray.
@property (nonatomic, strong) NSArray *itemsArray; //.h
@ synthesize itemsArray; //.m
8. Initialize your array.
itemsArray = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 6", nil];
9. Add these delegate and datasource methods of pickerview in your ViewController.m class:
#pragma mark - UIPickerView DataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [itemsArray count];
}
#pragma mark - UIPickerView Delegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30.0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [itemsArray objectAtIndex:row];
}
//If the user chooses from the pickerview, it calls this function;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Let's print in the console what the user had chosen;
NSLog(@"Chosen item: %@", [itemsArray objectAtIndex:row]);
}
10. Done! Hit Run!
Thank you. You make IOS programming easy.
ReplyDeleteVery good tutorials !!
Thank you! I hope it helped you. :)
Deletehow to add two picker View in one ViewController.xib
ReplyDeleteplz help me..!
Hi,
DeleteDeclare 2 pickerviews..
@property(nonatomic, strong) UIPickerview *myFirstPickerView;
@property(nonatomic, strong) UIPickerview *mySecondPickerView;
Synthesize them in your .m file.
then on your pickerView delegate and datasource methods.. you can see that all of the methods have the pickerview variable..
Example:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
There's a pickerView variable ^, right?
inside your method.. add an if-else statement if the pickerView is equal to pickerView 1 or 2...
if(pickerView == myFirstPickerView){
//your code for your first pickerview
}else{
//your coe for your second pickerview
}
how to add pickerView in actionsheet manually please guid
DeleteThanks for the great tutorial. I have a little bit more advanced question regarding Pickers. Let's say I have 4 text fields, each of which I want to use a PickerView for. On IB I only wish to drag one PickerView into the project, and yet use it for each text field. However, when the user taps on each text field, the picker should populate with different choices.
ReplyDeleteHow would I go about doing this given your tutorial above?
This comment has been removed by the author.
ReplyDeleteI am trying how to show the message in a label ....i have taken string and passed the string value to label but i am unable to solve can to help me....
ReplyDeletehi i want to used three picker on button click so how can i do that and i m creating uipicker programaticaly please help me thanks
ReplyDeletehi i want to show records from core data table
ReplyDeleteThanks Jenn! Your tutorials are always very concise and helpful.
ReplyDeleteIt would be nice if you would use a fixed-point font for your code snippets.
awesome tutorial
ReplyDeletehttp://quincetravels.com