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

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];
    //NSLog(title);
    return title;
}

Enclosed is my result. Just a bunch of rows with number? Result
LVL 5
thenrichAsked:
Who is Participating?
 
SpeedyApocalypseCommented:
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];
    //NSLog(title);
    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]];
    NSLog(@"%@",title);
    return title;

}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.