How do I add a date of birth UIDatePicker to UITextField?

George picture George · May 4, 2015 · Viewed 15.2k times · Source

I request the users date of birth on the sign up screen of my application. I can't figure out how to get a date of birth UIDatePicker to display. I tried using the following code but it only displays the date and time:

 UIDatePicker *dp = [[UIDatePicker alloc]init];
_textBirthday.inputView = dp;

Also, how would I set it so it only accepts users born after a certain year?

Answer

carlos16196 picture carlos16196 · May 4, 2015

1) in the .h file of your view controller make sure you assign textfield delegate:

@interface YourViewController : UIViewController <UITextFieldDelegate>

and also have an IBOutlet of the birthday Textfield

2) declare a date picker as a class variable to make it accessible from all different methods in class. in the .m file do the following after import and before implementation:

@interface YourViewController () {
UIDatePicker *datePicker;
}
@end

3) in viewdidload:

// make the textfield its own delegate
self.BirthdateTextfield.delegate = self;

// alloc/init your date picker, and (optional) set its initial date
datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]]; //this returns today's date

// theMinimumDate (which signifies the oldest a person can be) and theMaximumDate (defines the youngest a person can be) are the dates you need to define according to your requirements, declare them:

// the date string for the minimum age required (change according to your needs) 
NSString *maxDateString = @"01-Jan-1996";
// the date formatter used to convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// the specific format to use
dateFormatter.dateFormat = @"dd-MMM-yyyy";
// converting string to date
NSDate *theMaximumDate = [dateFormatter dateFromString: maxDateString];

// repeat the same logic for theMinimumDate if needed

// here you can assign the max and min dates to your datePicker 
[datePicker setMaximumDate:theMaximumDate]; //the min age restriction 
[datePicker setMinimumDate:theMinimumDate]; //the max age restriction (if needed, or else dont use this line)

// set the mode
[datePicker setDatePickerMode:UIDatePickerModeDate];

// update the textfield with the date everytime it changes with selector defined below
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

// and finally set the datePicker as the input mode of your textfield
[self.BirthdateTextfield setInputView:datePicker];

4) in same .m file, define the selector that will update textfield each time the date picker changes:

-(void)updateTextField:(id)sender {
    UIDatePicker *picker = (UIDatePicker*)self.BirthdateTextfield.inputView;
    self.BirthdateTextfield.text = [self formatDate:picker.date];
}

5) last but not least, this is the method called before assigning the date to the textfield.text (textfield.text needs a string not a date):

- (NSString *)formatDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}

enjoy coding!