Apparently populating a view picker is bigger deal then I thought.

Posted on 2011-10-14
Last Modified: 2012-05-12
First off let me say I'm a bit of a newbie but xCode makes me appreciate how far Microsoft has come in it's .NET development IDE and structure. xCode has many years of catch-up to compete with the big boys. Enough venting.

Anyway I'm simply trying to create and populate a view picker dynamically. Here is the code I have wired up behind a button:

- (void) CreateProjPicker

    [projectsArray addObject:@" Red "];
    [projectsArray addObject:@" Green "];
    [projectsArray addObject:@" Orange "];
    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    myPickerView.delegate = self;
    myPickerView.dataSource = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];    
    //[myPickerView reloadAllComponents];
    [myPickerView release];

Here are my delegate method:

//Picker view stuff
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return projectsArray.count;

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title;
    title = [@"" stringByAppendingFormat:@"%d",row];
    return title;

Enclosed is my result. Just a bunch of rows with number? Result
Question by:thenrich
    1 Comment
    LVL 13

    Accepted Solution

    Hi there,

    The issues that you are having is in your delegate method, where you send the object titles.

    Currently, you have this:

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        NSString *title;
        title = [@"" stringByAppendingFormat:@"%d",row];
        return title;

    Open in new window

    Specifically, look at your title assignment:

    title = [@"" stringByAppendingFormat:@"%d",row];

    Open in new window

    The picker is doing everything that you want it to do.  It is filling the string with the integer, "row" and then returning it.  Instead, you would like to retrieve the number from the array that you created in the -[CreateProjPicker] method.

    To do this, we will be using the -[objectAtIndex:] method of the NSArray class.  By the looks of it, you are using an NSMutableArray, but that will work fine too as it inherits from the NSArray class.

    Therefore, we end up with something like so:

    title = [NSString stringWithFormat:@"%@",[projectsArray objectAtIndex:row]];

    Open in new window

    Hence, your final code would look like so:

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        NSString *title = [NSString stringWithFormat:@"%@",[projectsArray objectAtIndex:row]];
        return title;

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
    Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now