views:

369

answers:

1

I am working on a core data using app and one of the views is EditingViewController, which acts as the controller for a number of ui elements which describe the attributes of objects. Inside the EditingViewController, all of my ui elements are being called and hidden with the .hidden = YES/NO; operation. One of my ui elements is a uipickerview. Currently, there is one array for each of two different views. Meaning that if they clicked on the first name field, array1 would load in picker1, and they would pick from it, and then if they clicked on last name, array2 would load in picker2, and life is good. Here is my code I am using to make this work up until now:

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

But! array1 is so big that I would like to add a UISegmentedControl to picker1 so that it can sort array1's info a little better. I know that UISegmentedControl is just a pretty set of buttons, requiring IBActions to be linked and stuff, but how could I implement it so that in one of my views, the first one, just picker1 and array1 are managed by the UISegmentedControl? Is it possible to make it so that it is just a smooth sorting process? I figure I will break down array1 into other arrays based on the conditions I set in my UISegmentedControl, how do I link those so they look and work good? Any help on this topic as always is greatly appreciated! Thanks

+1  A: 

Use the selectedSegmentIndex property.

For example:

enum {
    SEGMENT1,
    SEGMENT2,
    SEGMENT3
};

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if (picker1 == self.picker) {
        if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
            return [array1sub1 objectAtIndex:row];
        } else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
            return [array1sub2 objectAtIndex:row];
        } else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
            return [array1sub3 objectAtIndex:row];
        }
    } else {
        return [array2 objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (picker1 == self.picker) {
        if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
            return [array1sub1 count];
        } else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
            return [array1sub2 count];
        } else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
            return [array1sub3 count];
        }
    } else {
        return [array2 count];
    }
}

Also, you will want to reload the picker when the segmented control changes, so you should link the segmented control's value-changed event with a method like this:

- (IBAction)handleValueChanged
{
    [self.picker reloadAllComponents];
}
gerry3
That works perfectly! Thank you so much
Steve