top of page
  • Writer's pictureJennifer Eve Vega

UIPickerView Tutorial

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.

 3. Add a UIPickerView IBOutlet to your ViewController.h file, and synthesize it to your ViewController.m.

@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 (nonatomicstrong) 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!

#PickerView #UIPickerView #iPhone #InterfaceBuilder #UIPickerViewTutorial #iOSTutorial #mobile #PickerView #iOS

0 views0 comments

Recent Posts

See All

Hi Everyone! It has been a very long while since I last posted in this blog. This time, I will be making a tutorial in using blocks when we are accessing our API or web services. Disclaimer: I am also

Post: Blog2 Post
bottom of page