How to prepare UIPickerView with months and years in ios?

I am working on preparing a UIPickerView with months and years on 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 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

enter image description here

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.

+4
source share
6

.

.

.

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:

// 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;
}

2013 :

enter image description here

:

enter image description here

.

.

. .

+7

date and year, , UIDatePickerModeDate .

month and year, . UIPickerView.

, Mode property Date Picker Date (Cmd + Option + 4). ,

  datePicker.datePickerMode = UIDatePickerModeDate;

UIDatePickerModeDate

The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ]. 
Available in iOS 2.0 and later.

Declared in UIDatePicker.h.
+2

UIDatePickerView, , IB.

+1
source

use the UIDatePicker link to the apple , and the apple has a very good code example for using uidatepicker, and DateCell

+1
source

I also suggest you use UIDatePicker. This will be the best option.

+1
source

If you don't like using UIDatepicker, you can try this UIComponent (created by me), which displays only the month and year:

MonthSelector

+1
source

All Articles