如何在用户选择了他们的选择后解散一个UIPIckerView



我有我的UIPickerView钩到UITextField's inputView。我让它弹出。我知道用户什么时候选择了一行。我知道这一行的值。这一切都很棒。

我的问题是关于何时解散选择器。我曾见过有人将带有[取消]和[完成]的工具栏与选择器结合在一起,并在单击任意一个按钮时取消它。这可能是最好的方法,因为用户可以退出他们的选择。

我也见过一个应用程序,用户滚动到他们想要的选择,然后再次按下它来关闭选择器。我想知道怎么做。我注意到,一旦选择器通知您选定的特定行,如果用户继续单击/按相同的活动行,它将不再通知您。

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

嘿UserDano既然你已经添加了选择器作为ipnputView到文本字段,你可以调用

 [outletForTextField resignFirstResponder] 

,这将帮助你退出pickerView。现在,您想要这样做的方式是,在第一次选择时,您想要选择该值,而在第二次选择时(对于同一行),您想要隐藏Picker。因此,保留一个Bool值来存储所选值,然后在选择同一行时使用它来删除选择器可能会达到这个目的。希望能有所帮助

如果你想知道如何在单击按钮时取消选择器视图,你可以把我在我的一个项目中使用的这个代码片段:

    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

将上面的代码放入按钮IBAction方法中。