HowsThatMonkey HowsThatMonkey - 3 years ago 194
Objective-C Question

How do you create a custom UIDatePicker?

I am creating a health and fitness app for iOS. As part of the user registration they have to enter in their height. I am asking the user to do this by using a date picker but I am unsure as to how I would go about doing this. Anyone have any general tips or links that could guide me in the right direction?


Answer Source

Like rmaddy has alluded to, you will need to use a UIPickerView. UIDatePicker is for selecting dates / times only. Here's a link to the apple documentation:

And here's some tips to help get you started:

1) Create a property of type UIPickerView on your instance of UIViewController. I'm calling it pickerView for the purpose of demonstration.

2) Either alloc init the pickerView or link it to a pickerView on a storyboard / xib file.

3) Make sure you set the delegate of the pickerView to be your instance of UIViewController.

4) Make sure your instance of UIViewController implements the following protocols: UIPickerViewDataSource and UIPickerViewDelegate by adding this following line of code next to the interface declaration for your UIViewController subclass:

<UIPickerViewDataSource, UIPickerViewDelegate>

5) Actually implement the required methods (as well as whatever optional) of UIPickerViewDataSource and UIPickerViewDelegate. This is how you will tell your pickerView how many rows and columns it has, as well as what text to populate said rows and columns with.

Some sample code is shown below:

// MyViewController.h

@interface MyViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;


// MyViewController.m

@implementation MyViewController

- (void)viewDidLoad
    [super viewDidLoad];
    self.pickerView.delegate = self;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    // components are like columns. one column for feet and one for inches
    return 2;

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0){
        return [[self getUserDetailsHeightFeetOptions]count];
    } else {
        return [[self getUserDetailsHeightInchesOptions]count];

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(component == 0){
        return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightFeetOptions]objectAtIndex:row]];
    } else {
        return [NSString stringWithFormat:@"%@", [[self getUserDetailsHeightInchesOptions]objectAtIndex:row]];

- (NSArray*)getUserDetailsHeightFeetOptions{
    NSMutableArray *feetOptions = [[NSMutableArray alloc]init];
    for (int i = 3; i < 8; i++) {
        [feetOptions addObject:[NSNumber numberWithInt:i]];
    return feetOptions;

- (NSArray*)getUserDetailsHeightInchesOptions{
    NSMutableArray *inchesOptions = [[NSMutableArray alloc]init];
    for (int i = 0; i < 12; i++) {
        [inchesOptions addObject:[NSNumber numberWithInt:i]];
    return inchesOptions;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download