Search code examples
iosobjective-cuipickerviewuidatepicker

How to prepare UIPickerView with months and years in ios?


I'm working on Preparing UIPickerView with months and year in iOS.

Below is my code.

in viewdidload :

//Array for picker view
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:

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}

// tell the picker how many rows are available for a given component
- (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;
}


// tell the picker the width of each row for a given component
- (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 :

PickerImage

But in the current year , the months from Jan to Oct have expired. How to disable those years in my picker only for the current year dynamically. Those months should be available for the remaining years.

Actually the real output is,

enter image description here

In above, the expired months in the current year should be disabled in UI.

Any comments or suggestions would be appreciated.

Thank you in advance.


Solution

  • At final i wrote my logic to achieve it.

    But there may be a better way of logic to optimise memory.

    Below is my code.

    In viewdidload:

     currentDateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    
    //Array for picker view
    monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];
    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:[currentDateComponents month]-1 inComponent:0 animated:YES];
    [self.view addSubview:myPickerView];
    

    Picker view delegate methods :

    // tell the picker how many components it will have
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
    return 2;
    }
    
    // tell the picker how many rows are available for a given component
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
    NSInteger rowsInComponent;
    if (component==0)
    {
        rowsInComponent=[monthsArray count];
    }
    else
    {
        rowsInComponent=[yearsArray count];
    }
    return rowsInComponent;
    }
    
    
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        NSLog(@"[currentDateComponents month]-->%d<--",[currentDateComponents month]);
        NSLog(@"-->%d<--",row);
        NSLog(@"row->%@<--",[yearsArray objectAtIndex:row]);
        NSLog(@"-->%@<--",[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]]);
    
        if ([pickerView selectedRowInComponent:0]+1<[currentDateComponents month] && [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year])
        {
            [pickerView selectRow:[currentDateComponents month]-1 inComponent:0 animated:YES];
    
                    NSLog(@"Need to shift");
        }
    
    if (component==1)
    {
        [pickerView reloadComponent:0];
    }
    
    }
    
    
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:17];
    label.text = component==0?[monthsArray objectAtIndex:row]:[yearsArray objectAtIndex:row];
    
    if (component==0)
    {
        if (row+1<[currentDateComponents month] && [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year])
        {
            label.textColor = [UIColor grayColor];
        }
    }
    return label;
    }
    // tell the picker the width of each row for a given component
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
    {
    CGFloat componentWidth ;
    
    if (component==0)
    {
        componentWidth = 100;
    }
    else  {
        componentWidth = 100;
    }
    
    return componentWidth;
    }
    

    Here is my output

    for 2013 year:

    enter image description here

    for Other years:

    enter image description here

    .

    .

    . Thanks for your contribution.