lucas
lucas

Reputation: 443

uipickerview with 2 components delegate

I have the below code for picker with 2 components:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
    NSLog(@"first");
}
else
{
    NSLog(@"second");
}
}

the problem is when user try to scroll component 0 first and before waiting for it to completely stop, then scroll component 1 at the same time.

then i will just only have the console log of "second". So it looks like the first event got override by the second event.

is there a way that we can wait for the picker scroll events to complete itself?

Thanks.

Upvotes: 2

Views: 704

Answers (1)

Nikolay Suvandzhiev
Nikolay Suvandzhiev

Reputation: 9055

I had the same issue when trying to get a two-component UIPickerView to work. As someone suggested, do not use if-statements.

Instead do this:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.valueA = [self.valuePickerView selectedRowInComponent:0];
    self.valueB = [self.valuePickerView selectedRowInComponent:1];

    self.valueLabel.text = [NSString stringWithFormat:@"%i:%i", self.valueA, self.valueB];
}

Of course if you are looking for an element of an array, just use "objectAtIndex:selectedRowInComponent:0"


DO NOT DO THIS:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0){
        self.valueA = ...;
    }
    else if (component == 1){
        self.valueB = ...;
    }

    self.valueLabel.text = [NSString stringWithFormat:@"%i:%i", self.valueA, self.valueB];
}

Hope this helps anyone else struggling with this.

Upvotes: 2

Related Questions