-1

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?

Cheers!

2 Answers2

3

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:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerView_Class/index.html

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;

@end

// 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;
}
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
  • If this worked for you, please select it as the answer when you get a minute. Thanks. – Brian Sachetta Mar 31 '15 at 16:38
  • Sorry man, I can get this picker to display in my view controller. Am i missing a `[self.view addSubview: self.pickerView];` somewhere? – Declan Conway Apr 02 '15 at 16:13
  • I was assuming you dragged a picker view into your storyboard and wired it up. Hence the IBOultet property. If you don't do that, then yes, you need to manually allocate your picker and then call addSubview like you mentioned. – Brian Sachetta Apr 03 '15 at 02:03
0

There is a entire implementation here, if you like https://kmdarshan.wordpress.com/2015/05/03/creating-a-ios-type-date-picker-with-custom-text-in-it/

barryjones
  • 2,149
  • 1
  • 17
  • 21