I need a UIDatePicker
for selecting Month and Year only. I checked the class reference documents. Looks like UIDatePicker
is a UIView
.
I'm using a simple UIPickerView
with 2 components (months and years).
And calling the UIPickerViewDelegate
with every row selection:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Today year and month
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy"];
int year = [[dateFormat stringFromDate:[NSDate date]] intValue];
[dateFormat setDateFormat:@"MM"];
int month = [[dateFormat stringFromDate:[NSDate date]] intValue] - 1;
//picker year and month
int selectedYear = [[self.yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue];
int selectedMonth = [pickerView selectedRowInComponent:0];
// if month in the past, change the month
if (year == selectedYear && selectedMonth < month)
{
[pickerView selectRow:month inComponent:0 animated:YES];
}
}