views:

823

answers:

1

I have a core data based app I am working on, that uses an EditingViewController to control a number of different UI elements that get input from the user which describe attributes of an object that is subsequently stored within my app. The EditingViewController.xib file has such elements as a datePicker, textField, numberSlider, and where my problem is coming from a UIPickerView, that are all controlled within one view using .hidden = YES/NO; expressions. My problem is that I need to populate a UIPickerView in two seperate screens, that need to have two different NSMutableArray sources. Inside my viewDidLoad method I setup my first NSMutableArray:

listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:@"Greenland"];
[listOfItems addObject:@"Switzerland"];
[listOfItems addObject:@"Norway"];
[listOfItems addObject:@"New Zealand"];
[listOfItems addObject:@"Greece"];
[listOfItems addObject:@"Rome"];
[listOfItems addObject:@"Ireland"];

And after that, I populate my UIPickerView *picker with this code:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
    label.text= [listOfItems objectAtIndex:row];// The label is simply setup to show where the picker selector is at
}

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
    return 8;//[listOfItems count];

}

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
    return [listOfItems objectAtIndex:row];
}

And that works fine for the first attribute and array. So then after that, when a different uitableviewcell is selected, the picker is hidden picker.hidden = YES; and I need to have another picker, picker2 show up with a different array of information. But when I try and duplicate the process, by setting up a whole new picker, calling it picker2, and trying to populate it with a different NSMutableArray that I created right next to the first one (again, all because in my EditingViewController it's all part of the same view, just calling different UI elements depending on the view) I can't get picker2 to populate with the new array. I don't know how to set it up so that my two different arrays can be populated. Do I need two picker views? Can it be done with just one? What is the proper syntax in the - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; method to make the two arrays viewable seperately? I hope someone can help me out on this, thank you in advance!

+1  A: 

You are asking to use the same object as the delegate for two pickers.

In each of the delegate methods, the picker is provided as the first argument for this purpose. You can check which picker is passed in and return the appropriate data for that picker.

For example, if you add a property for the first picker named "pickerOne" and you second mutable array is called "arrayTwo", the delegate methods would look like this:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
    return 1; // assuming both pickers only have 1 component
}

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
    // The label is simply setup to show where the picker selector is at
    if (picker == self.pickerOne) {
        label.text= [listOfItems objectAtIndex:row];
    } else {
        label.text= [arrayTwo objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
    if (picker == self.pickerOne) {
        return [listOfItems count];
    } else {
        return [arrayTwo count];
    }
}

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
    if (picker == self.pickerOne) {
       return [listOfItems objectAtIndex:row];
    } else {
        return [arrayTwo objectAtIndex:row];
    }
}

Also, you can populate your array like this (if you just have a static list of strings, the array does not need to be mutable):

listOfItems = [[NSArray alloc] initWithObjects:@"Baptisms",@"Greenland",@"Switzerland",@"Norway",@"New Zealand",@"Greece",@"Rome",@"Ireland",nil];
gerry3
Wow, thank you! That was perfect!
Steve