I have a little code I was about to delete, but the snippet is better of it is online somewhere. Its not amazing, but it is searchable!
Objective-C code to create an array of all years since 1960. Perfect for input into a UIPicker
//Get Current Year into i2
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:@"yyyy"];
int i2 = [[formatter stringFromDate:[NSDate date]] intValue];
//Create Years Array from 1960 to This year
years = [[NSMutableArray alloc] init];
for (int i=1960; i<=i2; i++) {
[years addObject:[NSString stringWithFormat:@"%d",i]];
}
The UIPickerView Delegate Responses
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView*)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [years count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [years objectAtIndex:row];
}
Dont forget the declaration in the interface
//Data
NSMutableArray *years;
I put this section of code
ReplyDeleteNSDateFormatter* formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:@"yyyy"];
int i2 = [[formatter stringFromDate:[NSDate date]] intValue];
//Create Years Array from 1960 to This year
years = [[NSMutableArray alloc] init];
for (int i=1960; i<=i2; i++) {
[years addObject:[NSString stringWithFormat:@"%d",i]];
}
In my viewDidload for my viewController while the other section i put in my source code for my viewController and while its running its not changing anything. I created an outlet for a UIDatePicker and called it pickerView.
I'm very new at this.