1

I am using UIDatePicker in my application with UIDatePickerModeDate. I would like to use the same style of the DatePicker as in iOS contact address book where you can set date without year:

September | 11 | 2014
October | 12 | ----
November | 13 |

to get string in textField: October 12

Any ideas?

enter image description here

palec
  • 13
  • 5

2 Answers2

1

Just add a UIPickerView (NOT UIDatePickerView) and set the Datasource (and delegate) to your Class. Then in your Class add something like this:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
      switch (component) {
    case 0: // how many days? (you might find out how many days the month really had, instead of just returning 31
    {
#if TARGET_IPHONE_SIMULATOR

        return 50000;
        // I don't now, what the exact Value for NSIntegerMax is, but 50000 should be enough for testing.
#else

        return NSIntegerMax;
#endif

        break;
    }
    case 1: // how many months to display?
    {
#if TARGET_IPHONE_SIMULATOR

        return 50000;
#else

        return NSIntegerMax;
#endif

        break;
    }
    case 2:
    {
        return 100;  // how many years?
        break;
    }
}
return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) // days
    return [NSString stringWithFormat:@"%i",(int)row % 31 + 1];
else if (component == 1) // month
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM"];
    NSDate* myDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%i",(int)row % 12 + 1]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM"];
    return [formatter stringFromDate:myDate];
}


        else if (component == 2 && row == ([pickerView numberOfRowsInComponent:2] - 1))
    return @"-----";
       else
       {
            // let's use NSDateFormatter to get the current year:
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"YYYY"];

        return [NSString stringWithFormat:@"%i",[[dateFormatter stringFromDate:[NSDate date]] integerValue] - ([pickerView numberOfRowsInComponent:2] - 1) + (row + 1)];
        }
}

Result looks like this: resulting PickerView

You might implement the

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 

in order to change the width for each component.

Max Ka
  • 552
  • 4
  • 15
  • That works for me. Can you help me with making the first and second component endless? I mean to repeat values after the end. – palec Jan 12 '14 at 15:35
  • I changed my answer above. Please notice, that you can't make the picker "really" endless. It will stop after the user scrolls NSIntegerMax (which is a super high value and properly none will ever do so). I hope I could help. Would be nice if you could select my answer as the correct one ;) – Max Ka Jan 18 '14 at 11:47
0

The UIDatepickerModeDate will not let you get the required format of date you are expecting. So either user a UIPickerView & customize it got dates or else, follow the trick from the above answer.

Also you can be interested in reading this.

link 1

Hope that helps.

Community
  • 1
  • 1
Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41