15 Oct 2015 0 comments

Unite is a conference that is run by Unity Technologies to promote their key product, the Unity gaming engine. It isn’t so much as a conference, but rather an experience. With a keynote being pre...

Read More
15 Oct 2015 0 comments

While I worked on creating the iOS Animations by Tutorials video series and the three editions of the iOS Animations by Tutorials book I got a lot of ideas how the existing anim...

Read More
15 Oct 2015 0 comments

Happy Wednesday – it’s time for our 3rd new book release as part of the iOS 9 Feast! Today, Aaron, Saul, Matthew, Pietro and I are happy to announce that Core Data by Tutorials Second E...

Read More
15 Oct 2015 0 comments

In this getting started with Core Data tutorial, you’ll write your very first Core Data app using Swift 2.0. You’ll see how easy it is to get started with all the resources provided in Xcode, fro...

Read More
15 Oct 2015 0 comments

When you create a Core Data app, you design an initial data model for your app. However, after you ship your app inevitably you’ll want to make changes to your data model. What do you do then – you...

Read More
15 Oct 2015 0 comments

As part of this year’s iOS 9 Feast, we are releasing a new video tutorial series every Thursday. This week, we are happy to release a brand new video tutorial series – Introducing Custom Cont...

Read More
15 Oct 2015 0 comments

There’s been a big hole in Spotlight on iOS for a long time. Although users can use it to find you app, they can’t see inside it — to all the content that they really care about. Currentl...

Read More
15 Oct 2015 0 comments

Storyboards have been around since iOS 5 and have received lots of upgrades and new features since then, including unwind segues for reverse navigation, universal storyboards for both iPhone and iP...

Read More

Latest Posts:

UIPicker iPhone SDK - Populated with Year


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 allocinit]autorelease];
[formatter setDateFormat:@"yyyy"];
int i2  = [[formatter stringFromDate:[NSDate date]] intValue];


//Create Years Array from 1960 to This year
years = [[NSMutableArray allocinit];
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;

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments:

  1. I put this section of code

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

    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.

    ReplyDelete