I am working on preparing a UIPickerView with months and years on iOS.
Below is my code.
in viewdidload :
monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *yearString = [formatter stringFromDate:[NSDate date]];
yearsArray=[[NSMutableArray alloc]init];
for (int i=0; i<13; i++)
{
[yearsArray addObject:[NSString stringWithFormat:@"%d",[yearString intValue]+i]];
}
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[myPickerView selectRow:0 inComponent:0 animated:YES];
[self.view addSubview:myPickerView];
Picker view delegate methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger rowsInComponent;
if (component==0)
{
rowsInComponent=[monthsArray count];
}
else
{
rowsInComponent=[yearsArray count];
}
return rowsInComponent;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString * nameInRow;
if (component==0)
{
nameInRow=[monthsArray objectAtIndex:row];
}
else if (component==1)
{
nameInRow=[yearsArray objectAtIndex:row];
}
return nameInRow;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
CGFloat componentWidth ;
if (component==0)
{
componentWidth = 100;
}
else {
componentWidth = 100;
}
return componentWidth;
}
And I got the following output:

But this year, the months from January to October have expired. How to disable these years in my collector only for the current year dynamically. These months should be available for the remaining years.
In fact, the real conclusion

In the above, elapsed months in the current year should be disabled in the user interface.
Any comments or suggestions would be appreciated.
Thanks in advance.