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

Posted on 2011-10-14
Medium Priority
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

SpeedyApocalypse earned 2000 total points
ID: 36973379
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


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
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.
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 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