UIPicker for month and year. Getting years dynamically
dynamic, ios, objective-c, uipickerview
Solution
Extract the current year from an `NSDate` object. Then build the year list using a loop.
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comps = [gregorian components:NSYearCalendarUnit fromDate:now];
int year = [comps year];
NSMutableArray *years = [NSMutableArray array];
for (int y = year; y < year + 20; y++) {
[years addObject:@(y)];
}
Change the `20` for however many years you want in the list.
Problem
I am trying to make a picker with month and year only (for credit card expiration date). After searching around, it seems that using a custom UIPicker would be the best option. My question is, is there a way to get the array for years dynamically, without having to hardcode all of the upcoming years? I have been looking around, but can't seem to find an answer. If anyone could point me in the right direction, that would be greatly appreciated. Thank you.