ios UIPickerView 作为 UITextField 的 inputView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20740874/
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 as inputView of UITextField
提问by SilverHood
I've read a lot about how I can use a UIPickerView
as the inputView
of my UITextField
. The thing is, I am able to call the UIPickerView
when I tap on the UITextField
. However, my app always loads with the UIPickerView
shown. I have already tried changing the myownpickerview.hidden = YES;
in viewDidLoad
, but this causes problems when I click on the UITextField
. It won't show up, and if I click multiple times, the debugger shows that there would be an error.
我已经阅读了很多关于如何使用 aUIPickerView
作为inputView
我的UITextField
. 问题是,UIPickerView
当我点击UITextField
. 但是,我的应用程序始终加载UIPickerView
显示。我已经尝试过更改myownpickerview.hidden = YES;
in viewDidLoad
,但是当我单击UITextField
. 它不会出现,如果我多次单击,调试器会显示会出现错误。
Can anyone point me in the right direction? I only want the UIPickerView
shown after I tap on the UITextField
任何人都可以指出我正确的方向吗?我只想要UIPickerView
在我点击后显示UITextField
I'm still working on my first ever iOS app. Please be patient. Thank you =)
我仍在开发我的第一个 iOS 应用程序。请耐心等待。谢谢你=)
回答by Aklesh Rathaur
Try this, it works fine, put it in viewdidload.
试试这个,它工作正常,把它放在viewdidload中。
yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
[yourpicker setDataSource: self];
[yourpicker setDelegate: self];
yourpicker.showsSelectionIndicator = YES;
self.yourtextfield.inputView = yourpicker;
do not[self.view addSubview: yourpicker];
this
不要[self.view addSubview: yourpicker];
这个
回答by Sureshkumar Linganathan
Use like this,
像这样使用,
// Picker
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,150)];
picker.dataSource = self;
picker.delegate = self;
// Tool bar
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,35)];
pickerToolbar.barStyle = UIBarStyleDefault;
// Bar button
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneButtonTapped:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[pickerToolbar setItems:@[flexSpace,doneButton]];
self.selectedLockerTextFiled.inputAccessoryView = pickerToolbar;
self.selectedLockerTextFiled.inputView = picker;
Below code for done button actions,
下面是完成按钮操作的代码,
-(void)pickerDoneButtonTapped:(id)picker{
[self.view endEditing:YES];}