7

I have my UIPickerView hooked into a UITextField's inputView. I have it popping up. I know when the user selects a row. I know the value for the row. All that is great.

My question deals with when to dismiss the picker. I've seen people marry a toolbar with [Cancel] and [Done] to the picker and dismiss it on either button click. This is probably the best approach as the user can back out of their choice.

I've also seen one app where the user scrolls to the selection they want and then presses it again to dismisses the picker. I would like to know how to do this. I have noticed that once the picker informs you of a specific row being selected, it will not inform you again if the user continues to click/press on the same active row.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Userdano
  • 81
  • 1
  • 5

2 Answers2

2

Hey UserDano since you have added the picker as an ipnputView to the text field you can call

 [outletForTextField resignFirstResponder] 

and this will help you resign the pickerView . Now The way you want to do this is on first selection you want to Select the value and on second selecteion (for same row) you want to hide the Picker.So keeping a Bool value to store the selected value and later using it to remove the picker when same row is selected might do the trick. Hope it helps

Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47
  • Rahul, thanks for the response. My problem is knowing when the user selects the same row twice in a row - like a double tap. First the user will scroll to the row they want, then once done scrolling, they tap on it. The picker only sends the didSelectRow message when the scrolling is done, not when they tap/reselect. I've verified this on my iPhone 4 and the iPhone 4.3 Simulator. I'm guessing I have to go to a lower level and maybe handle Touch events? LOL, I was hoping this would be something stupid I missed. – Userdano Oct 12 '11 at 02:33
  • There is one more method to grab the double tap. i.e to add a overlapping maskview just above the row that user scrolls to. once user double taps the selected row touches are grabbed by maskview and in turn we can select row,remove maskview and dismiss picker. – Rahul Sharma Oct 12 '11 at 04:11
  • Thanks for the idea. I will try that out. I haven't used a maskview yet but that should not stop me from learning. – Userdano Oct 12 '11 at 19:24
0

If you want to know how to dismiss a picker view when click on a button, you can put this snippet i have used in one of my projects:

    NSInteger selectedRow=[yourPickerView selectedRowInComponent:0];// i assume your picker have one component
NSString *item=[yourPickerView objectAtIndex:selectedRow];//get the selected item
yourTextField.text=[NSString stringWithFormat:@"%@",item];//display the selected item in the text field
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 480);
yourPickerView.transform=transform;
[UIView commitAnimations];//dismiss the view controller which contains the picker view

Put the code above in your button IBAction method. Hope that helps.

Luca
  • 20,399
  • 18
  • 49
  • 70