繁体   English   中英

UIPickerView作为UITextField的inputView

[英]UIPickerView as inputView of UITextField

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

谁能指出我正确的方向? 我只想在点击UITextField后显示UIPickerView

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

试试这个,它工作正常,把它放在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]; 这个

像这样使用,

 // 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];}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM