ios UIPickerView 选择和隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5347537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIPickerView select and hide
提问by Tofu
How do you make a UIPickerView
act like the one with a webview wherein there is a drop down selection box and instead of dropping down like usual websites do, the iphone
makes it into a UIPickerView
with all the selections in. When you select one, a check becomes visible beside your selection and changes the value of the drop box. And how do you put the "Done" button on top of the UIPickerView
to dismiss the UIPickerView
?
你如何做出一个UIPickerView
像 webview 那样的行为,其中有一个下拉选择框,而不是像通常的网站那样下拉,而是把iphone
它变成一个UIPickerView
包含所有选择的。 当你选择一个时,一个检查变得可见在您的选择旁边并更改下拉框的值。以及如何将“完成”按钮放在 顶部UIPickerView
以关闭UIPickerView
?
I already know that [pickerview setHidden:YES]
is the method to use to hide the pickerview. I just don't know how to include the "Done" button in the UIPickerView
.
我已经知道这[pickerview setHidden:YES]
是用于隐藏选择器视图的方法。我只是不知道如何在UIPickerView
.
Regards, Chris
问候, 克里斯
采纳答案by Jhaliya
The "Done" button is placed in UIToolBar.
“完成”按钮放置在 UIToolBar 中。
Use the below method of UIToolBar for adding the "Done" buttons.
使用 UIToolBar 的以下方法添加“完成”按钮。
- (void)setItems:(NSArray *)items animated:(BOOL)animated {
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
}
回答by Jacob T.
This piece of code will slide out a picker view as keyboard and attached a done button on top of it. Basically, you want to set a inputAccessoryView with your input field. You should call this method on a touch down event for your input field.
这段代码将作为键盘滑出一个选择器视图,并在其顶部附加一个完成按钮。基本上,您想使用输入字段设置 inputAccessoryView。您应该在输入字段的触摸事件上调用此方法。
- (IBAction)showYourPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIPickerView* pickerView = [[UIPickerView alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
self.yourPickerView = pickerView; //UIPickerView
yourTextField.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(pickerDoneClicked:)] autorelease];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
yourTextField.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
Finally, your Done button calls the "pickerDoneClicked" method, where you should add
[yourTextField resignFirstResponder];
which will hide the picker view.
最后,您的完成按钮调用“pickerDoneClicked”方法,您应该在[yourTextField resignFirstResponder];
其中添加
隐藏选择器视图的方法。