Peter Warbo
Peter Warbo

Reputation: 11700

Objective-C – UIDatePicker with custom values

So ideally I would like to use the UIDatePicker to display dates. But instead of displaying a specific day (for the leftmost component) like Feb 2 I would like it to display a custom value like i.e Peter's birthday.

Is there a way to achieve this with using the UIDatePicker? Or do I have to create a UIPickerView and feed it with a datasource of dates and my custom values?

If so how would I go about feeding it with dates (what would my dataSource look like)?

Upvotes: 1

Views: 1680

Answers (1)

Christian Schnorr
Christian Schnorr

Reputation: 10776

UIDatePicker only allows you do pick dates in som predefined styles like date,time,date+time etc..., so you would have to go with a UIPickerView.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0: return self.birthdays.count;
        case 1: return 24;
        case 2: return 60;
        default: return -1;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0: return [[self.birthdays objectAtIndex:row] title];
        case 1: return [NSString stringWithFormat:@"%d", row];
        case 2: return [NSString stringWithFormat:@"%d", row];
        default: return nil;
    }
}

Upvotes: 1

Related Questions