UIPickerView作为UITextField的inputView

时间:2013-12-23 09:48:18

标签: ios iphone objective-c uitextfield uipickerview

我已经阅读了很多关于我如何使用UIPickerView作为inputView的{​​{1}}的内容。问题是,当我点击UITextField时,我可以拨打UIPickerView。但是,我的应用始终加载显示的UITextField。我已经尝试更改UIPickerView中的myownpickerview.hidden = YES;,但是当我点击viewDidLoad时会出现问题。它不会显示,如果我多次单击,调试器会显示错误。

有人能指出我正确的方向吗?我只想点击UITextField

后显示UIPickerView

我还在开发我的第一款iOS应用程序。请耐心等待。谢谢=)

3 个答案:

答案 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;

不要忘记实现 UIPickerViewDataSourceUIPickerViewDelegate