开发者

How to Dismiss a UIPIckerView once the user has selected their choice

开发者 https://www.devze.com 2023-04-12 16:10 出处:网络
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.

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.

- (v开发者_Python百科oid)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消