我已经阅读了很多关于我如何使用UIPickerView
作为inputView
的{{1}}的内容。问题是,当我点击UITextField
时,我可以拨打UIPickerView
。但是,我的应用始终加载显示的UITextField
。我已经尝试更改UIPickerView
中的myownpickerview.hidden = YES;
,但是当我点击viewDidLoad
时会出现问题。它不会显示,如果我多次单击,调试器会显示错误。
有人能指出我正确的方向吗?我只想点击UITextField
UIPickerView
我还在开发我的第一款iOS应用程序。请耐心等待。谢谢=)
答案 0 :(得分:47)
试试这个,它运行正常,把它放在viewdidload中。
yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
[yourpicker setDataSource: self];
[yourpicker setDelegate: self];
yourpicker.showsSelectionIndicator = YES;
self.yourtextfield.inputView = yourpicker;
不 [self.view addSubview: yourpicker];
此
答案 1 :(得分:0)
像这样使用
// 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;
以下完成按钮操作的代码,
-(void)pickerDoneButtonTapped:(id)picker{
[self.view endEditing:YES];}
答案 2 :(得分:-1)
使用 Swift:
let pickerView = UIPickerView()
pickerView.dataSource = self
pickerView.delegate = self
textField.inputView = pickerView;
不要忘记实现 UIPickerViewDataSource
和 UIPickerViewDelegate
。