Search code examples
iphoneiosuipickerview

Set range of a UIPickerView component


Is there a way to set the range of allowable selections in a UIPickerView?

I have a UIPickerView with 2 components each representing a musical note value. One is the lower note. One is the upper note. I want to ensure that the user doesn't make the upper note lower than the lower one and vice versa. Make sense? I'd prefer not to have to update the picker values each time either value changes. Any thoughts?

screenshot of UIPickerView


Solution

  • You could just check it in the didSelectRow method and revert if necessary. You'll need to store the row in a property.

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        //pseudocode
        if(RowIsIllegalTest)
        {
            //[pickerViewOne selectRow:pickerViewRow inComponent:(what component is it in?) animated:YES]
        }
        else
        {
            //update the row for next time around
            self.pickerViewRow = row;
        }
    return;
    

    }

    edit: you'll also need to keep the stored pickerView rows seperate