ios 选择 UITextField 时如何显示 UIPickerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7077258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:07:34  来源:igfitidea点击:

How to Show UIPickerView when selecting UITextField

iosobjective-cuitextfielduipickerview

提问by sillersam

Here is a screenshot of what I did till now:

这是我到目前为止所做的截图:

enter image description here

在此处输入图片说明

So what I am trying to do is when you select "pick a name" Textfield I need a Picker to show up, with the input @"Hyman".

所以我想要做的是当你选择“选择一个名字”文本字段时,我需要一个选择器来显示,输入@“Hyman”。

采纳答案by PJR

it will work for you .. i have edited it .and for that you have to set delegate for textfield. and create a UIPIckrView in NIb file.

它会为你工作..我已经编辑了它。为此你必须为文本字段设置委托。并在 NIb 文件中创建一个 UIPIckrView。

- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
    pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width,    pickrView.frame.size.height);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.50];
    [UIView setAnimationDelegate:self];
    pickrView.frame = CGRectMake(0, 200, pickrView.frame.size.width, pickrView.frame.size.height);
    [self.view addSubview:pickrView];
    [UIView commitAnimations];
    return NO;
}

回答by Tom

Since iOS 3.2, UITextFieldsupports the inputViewproperty to assign a custom view to be used as a keyboard, which provides a way to display a UIPickerView:

从 iOS 3.2 开始,UITextField支持inputView分配自定义视图用作键盘的属性,它提供了一种显示UIPickerView:

You could use the inputViewproperty of the UITextField, probably combined with the inputAccessoryViewproperty. You assign your pickerViewto the inputViewproperty, and, to dismiss the picker, a done button to the inputAccessoryViewproperty.

您可以使用 的inputView属性UITextField,可能与inputAccessoryView属性结合使用。您将您的分配pickerView给该inputView属性,并为该属性设置一个完成按钮以关闭选择器inputAccessoryView

UIPickerView *myPickerView = [[UIPickerView alloc] init];
//myPickerView configuration here...
myTextField.inputView = myPickerView;

Like that. This will not give you a direct way to dismiss the view since your UIPickerViewhas no return button, which is why I recommend to use the inputAccessoryViewproperty to display a toolbar with a done button (the bar is just for aesthetics, you might as well just use a UIButtonobject):

像那样。这不会为您提供直接关闭视图的方法,因为您UIPickerView没有返回按钮,这就是为什么我建议使用该inputAccessoryView属性来显示带有完成按钮的工具栏(该栏仅用于美观,您不妨使用一个UIButton对象):

UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
 CGRectMake(0,0, 320, 44)]; //should code with variables to support view resizing
UIBarButtonItem *doneButton =
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
 target:self action:@selector(inputAccessoryViewDidFinish)];
 //using default text field delegate method here, here you could call
 //myTextField.resignFirstResponder to dismiss the views
[myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
myTextField.inputAccessoryView = myToolbar;

回答by thesummersign

I use this and find this a lot cleaner than adding a subview and animating the UIPicker

我使用它并发现它比添加子视图和动画更清晰 UIPicker

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
responder = textField;

    if ([textField isEqual:self.txtBirthday]) {
    UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
    [datepicker setDatePickerMode:UIDatePickerModeDate];

    textField.inputView = datepicker;
    }

    return YES;
}

回答by Faizan S.

Well, you could rely on the UITextFieldDelegateto handle this kind of functionality.

好吧,您可以依靠UITextFieldDelegate来处理这种功能。

Inside the

在 - 的里面

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

is where you would set the text of your current UITextFieldas well as initializing and showing the UIPickerView.

是您设置当前文本UITextField以及初始化和显示UIPickerView.

Important notice:

重要通知:

You might also want to conform to the UIPickerViewDelegate.

您可能还希望符合UIPickerViewDelegate.

HTH

HTH

回答by Hemang

Swift:

迅速:

internal var textFieldHandlerToolBar: UIToolbar = {
    let tb = UIToolbar.init(frame: CGRect.init(origin: .zero, size: CGSize.init(width: UIScreen.main.bounds.width, height: 44.0)))
    let doneBarButton = UIBarButtonItem.init(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(actionDonePickerSelection))
    tb.setItems([doneBarButton], animated: false)
    return tb
}()

internal var pickerView: UIPickerView = {
    let pv = UIPickerView.init()
    return pv
}()

@objc internal func actionDonePickerSelection() {
     textField.resignFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.pickerView.delegate = self
    self.pickerView.datasource = self
}

Use it like this:

像这样使用它:

textField.inputAccessoryView = self.textFieldHandlerToolBar
textField.inputView = self.pickerView

回答by Nitish

What you can do is, create a UIButtonwith custom type on UITextField. Both having equal sizes. On the touch of button you can show UIPickerView.

您可以做的是,UIButtonUITextField. 两者具有相同的大小。在触摸按钮,你可以显示UIPickerView

回答by Mannam Brahmam

ViewController.h

视图控制器.h

@interface ChangeCurrencyVC : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
      NSArray *availableCurreniesArray;
}
@property (weak, nonatomic) IBOutlet UITextField *chooseCurrencyTxtFldRef;

ViewController.m

视图控制器.m

 - (void)viewDidLoad {
[super viewDidLoad];
availableCurreniesArray = @[@"Indian Rupee", @"US Dollar", @"European Union Euro", @"Canadian Dollar", @"Australian Dollar", @"Singapore Dollar", @"British Pound", @"Japanese Yen"];
// Do any additional setup after loading the view.
[self pickerview:self];
}

 #pragma mark -  picker view Custom Method
 -(void)pickerview:(id)sender{
  UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

// set change the inputView (default is keyboard) to UIPickerView
self.chooseCurrencyTxtFldRef.inputView = pickerView;

// add a toolbar with Cancel & Done button
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.chooseCurrencyTxtFldRef.inputAccessoryView = toolBar;
}
 #pragma mark - doneTouched
- (void)cancelTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.chooseCurrencyTxtFldRef resignFirstResponder];
 }
  #pragma mark - doneTouched
- (void)doneTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.chooseCurrencyTxtFldRef resignFirstResponder];
// perform some action
 }
#pragma mark - The Picker Challenge
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [availableCurreniesArray count];
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component{
return availableCurreniesArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.chooseCurrencyTxtFldRef.text = availableCurreniesArray[row];
}

回答by KVISH

http://tmblr.co/ZjkSZteCOUBS

http://tmblr.co/ZjkSZteCOUBS

I have the code and everything laid out in my blog to do this exactly. But below, I have the basic concept laid out.

我在我的博客中有代码和所有内容,可以准确地做到这一点。但在下面,我已经列出了基本概念。

Basically the solution involves an opensource project called ActionSheetPicker on github, and implementing the function textFieldShouldBeginEditingon the UITextFieldDelegate. You can dismiss the keyboard there and provide a UIPickerView instead. The basic code is listed here:

基本上,该解决方案涉及 github 上一个名为 ActionSheetPicker 的开源项目,并textFieldShouldBeginEditingUITextFieldDelegate. 您可以在那里关闭键盘并提供 UIPickerView 代替。基本代码如下:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // We are now showing the UIPickerViewer instead

    // Close the keypad if it is showing
    [self.superview endEditing:YES];

    // Function to show the picker view
    [self showPickerViewer :array :pickerTitle];

    // Return no so that no cursor is shown in the text box
    return  NO;
}