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 :
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,
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.
If you do not like to use UIDatepicker then you can try this UIComponent (created by me) which displays only month and year:
MonthSelector
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:
Picker view delegate methods :
Here is my output
for 2013 year:
for Other years:
.
.
. Thanks for your contribution.
if Your question mentions
date and year
so it seemed likeUIDatePickerModeDate
would suffice .you are looking for
month and year
which is not an available option. I suggest you consider using a two componentUIPickerView
object.Original Answer
You can do this by changing the
Mode property
underDate Picker
to Date in the Attributes Inspector in the right bar ( Cmd + Option + 4 ). You can also do this programmatically,use UIDatePicker apple reference and apple has a very good sample code to use uidatepicker as well DateCell
You can use the UIDatePickerView, and then you can specify the format that is being show from the IB.
I also suggest you to use UIDatePicker instead. That will be a better option.