views:

622

answers:

2

What I would like to do is this:

A UIPickerView is shown. If the user touches the selected row, the row is locked (it is a multi-component picker) and the other components are free to spin. If the row has already been locked and the user touches the locked row, the row is then unlocked and free to spin. I have the locking part coded already using a button. I would like to remove the button and replace with the highlighted picker option.

I have tried:

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

Apparently this only fires if the row has not been selected already so when I touch a row that is in the highlighted region, this event does not fire.

I then tried

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 NSLog(@"touchesBegan");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 NSLog(@"touchesMoved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 NSLog(@"touchesEnded");
}

None of these events fire when the picker is touched.

Any ideas on how to detect when a highlighted/selected row in a picker is touched by the user?

A: 

Well -- there is a simple workaround that did exactly what I wanted to accomplish. Basically I wanted to have the user tap the selection bar on a multicomponent picker view and have that component locked while the others are free to spin.

Here is what I did:

First - turn off the option to show the selection bar.

Second - create three labels - one for each component -- the labels are the same height and location as the selector bar, but there is one over each component. They but each other to appear to be a solid bar.

Third - create a method to change the color of the label to indicate that it is locked to the user. I am also using a boolean flag to let the program processes know when a component is locked.

    - (IBAction) lockButtonPress:(id)sender {

    // determine which button was pressed....
    int btnPressed = 0;
    if (leftSelectionBar.touchInside) btnPressed = 1;
    if (centerSelectionBar.touchInside) btnPressed = 2;
    if (rightSelectionBar.touchInside) btnPressed = 3;

    // we are not going to make this difficult -- images for different states..... default in viewWillShow
    switch (btnPressed) {
     case 1:
      if (lockSelected0) {
       lockSelected0 = FALSE;
       [leftSelectionBar setBackgroundColor:[UIColor blueColor]];
       [leftSelectionBar setAlpha:0.25];
      } else {
       lockSelected0 = TRUE;
       [leftSelectionBar setBackgroundColor:[UIColor redColor]];
       [leftSelectionBar setAlpha:0.45];

      }
      break;
     case 2:
      if (lockSelected1) {
       lockSelected1 = FALSE;
       [centerSelectionBar setBackgroundColor:[UIColor blueColor]];
       [centerSelectionBar setAlpha:0.25];
      } else {
       lockSelected1 = TRUE;
       [centerSelectionBar setBackgroundColor:[UIColor redColor]];
       [centerSelectionBar setAlpha:0.45];
      }
      break;
     case 3:
      if (lockSelected2) {
       lockSelected2 = FALSE;
       [rightSelectionBar setBackgroundColor:[UIColor blueColor]];
       [rightSelectionBar setAlpha:0.25];
      } else {
       lockSelected2 = TRUE;
       [rightSelectionBar setBackgroundColor:[UIColor redColor]];
       [rightSelectionBar setAlpha:0.45];
      }
      break;
     default:
      break;
    } 
}

And that's it.... simple ;)

iPhone Guy
A: 

First, I am new to app. development. I have tried to implement

- (IBAction) lockButtonPress:(id)sender { 

    // determine which button was pressed.... 
    int btnPressed = 0; 
    if (leftSelectionBar.touchInside) btnPressed = 1; 
    if (centerSelectionBar.touchInside) btnPressed = 2; 
    if (rightSelectionBar.touchInside) btnPressed = 3; 
    ...
    ...
}.

One thing puzzles me. How is the user's touches detected if UILabels are creaetd for each of the picker components? I understand how to detect the user's touches on the picker, but these labels overlay each picker component. My method is not being called after I change the values in the picker components. Any suggestions?

yellahd
You should post follow-up question as a separate thread, notas an answer. After all, it doesn't really answer *this*question. Also more people would see it if you post it asyour own question.
sth