2

I want to do somenthing with the UIPickerView but I simple can't see how I can solve this problem...

This example is similar to what I want, and easily understandable. I have two simple pickers, with numbers from 0 to 10. Then I have a textfield where the user inserts a number.

What I want is that when the users inserts, for example, 7, and then selects a number in one of the pickers, like 5, the other pickers moves to 2, so that the sum is 7. The same with 0 and 7, 3 and 4.

Basically, how to make a picker move, according to the other picker value selected, based on the input inserted before.

Thank you very much :)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Tomsss
  • 63
  • 6

3 Answers3

0

try it out suppose you have two UIPickerView like pickerview1 and pickerview2 then you can set the logic like bellow..

Note 1.:- If you used different 2 UIPickerView then use bellow code..

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (thePickerView == pickerview1) {
        if ([yourTextfield.text integerValue] > row) {
            int selectVal = [yourTextfield.text integerValue] - row ;
            [pickerview2 selectRow:selectVal inComponent:0 animated:YES];
        }
        else{
            int selectVal = row - [yourTextfield.text integerValue] ;
            [pickerview2 selectRow:selectVal inComponent:0 animated:YES];
        }
    }  
}

Here what you get just see here if you entered value in UITextField 7 and then select value 5 of pickerview1 then its row number is 4.

here when you minus that 4 from the 7 then what you get is 3 after you set or Select the row number is 3 of pickerview2 and its value is 2.

Note 2.:- If you used 1 UIPickerView and 2 components then use bellow code..

   - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        if (component == 0) {
            if ([yourTextfield.text integerValue] > row) {
                int selectVal = [yourTextfield.text integerValue] - row ;
                [thePickerView selectRow:selectVal inComponent:1 animated:YES];
            }
            else{
                int selectVal = row - [yourTextfield.text integerValue] ;
                [thePickerView selectRow:selectVal inComponent:1 animated:YES];
            }
        }  
    }

So you got exact output which you want..

i hope you got it...

Enjoy the coding.. :)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • @Tomsss you have 2 `UIPickerView` or 2 `components`?? – Paras Joshi May 27 '13 at 12:11
  • But how would you do if instead of having the numbers like 0, 1,2,3,4,5,6,... You have any other order? Isn't possible to say "I want "2" to appear in the other picker", instead of saying "I want the object at index x"? – Tomsss May 27 '13 at 16:18
  • yes if you have 2 arrays then create that array with 0 to 10 numbers digit – Paras Joshi May 28 '13 at 03:47
  • Sorry, I did't understand. If I had an array with the object @"3" at the index 14, how could I say to the picker "show me "3"" instead of "show me what is at index 14"? – Tomsss May 29 '13 at 13:03
0

u add array and reload component like this:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (component == 0) {
        club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
      [secondcomponearray addObject club];
        [pickerView reloadComponent:1];

}
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
0

Code modified from this post: find pair of numbers in array that add to given sum

The code below loops through to find the possible pairs adding up to the number (the number typed into the textfield), and then in the picker selects the rows with the numbers equalling one of the numbers in the pair. The code assumes that your data source contains the numbers from 0 up to some total in ascending order.

NSInteger number = [[textField text] integerValue];

int i = 0;
int j = number-1
NSMutableArray *possiblePairs = [NSMutableArray array];
while(i < j){
   if (i + j == number) {
      possiblePairs[] = @[(i), @(j)];
   }
   else if (a[i] + a[j] <  number) i += 1;
   else if (a[i] + a[j] >  number) j -= 1;
}

[picker1 selectRow:[dataSource indexOfObject:@(i)] inComponent:0];
[picker2 selectRow:[otherDataSource indexOfObject:@(j)] inComponent:0];
Community
  • 1
  • 1
max_
  • 24,076
  • 39
  • 122
  • 211