简体   繁体   English

如何在iPhone中将UIPickerView设置为inputView到UITextField

[英]How to set UIPickerView as inputView to UITextField in iPhone

I am trying to open UIPickerView as inputView to UITextfield . 我正在尝试将UIPickerView作为inputView打开到UITextfield

Here is my code in textFieldDidBeginEditing : 这是我在textFieldDidBeginEditing代码:

  UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];
    categoryPicker.delegate = self;
    categoryPicker.dataSource = self;
    [categoryPicker  setShowsSelectionIndicator:YES];
    txtCategory.inputView =  categoryPicker;

  UIToolbar *_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
    _providerToolbar.barStyle = UIBarStyleBlackOpaque;
    [_providerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet)];

    [barItems addObject:doneBtn];

    [_providerToolbar setItems:barItems animated:YES];
    txtCategory.inputAccessoryView = _providerToolbar;

It's working fine for first time, but when I am selecting any value from UIPickerView it's hiding and my UITextfield is editable and UIPickerView is not showing at that time. 第一次工作正常,但是当我从UIPickerView选择任何值时,它正在隐藏并且我的UITextfield是可编辑的,而UIPickerView当时没有显示。 What am I doing wrong? 我究竟做错了什么?

I'd suggest you to move your code to viewDidLoad: . 我建议您将代码移至viewDidLoad: Because textFieldDidBeginEditing: is called when textField loads the keyboard for it. 因为textFieldDidBeginEditing:在textField为其加载键盘时被调用。 So It is not the right time to change the inputAccessoryView of the textField. 因此,现在不是更改textField的inputAccessoryView的时候。

Use this...

    NSDate *now = [NSDate date];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

    [dateFormat setDateFormat:@"dd MMM, yyyy"];

    UIDatePicker *datePicker = [[UIDatePicker alloc] init];

    [datePicker setMaximumDate:now];

    datePicker.datePickerMode = UIDatePickerModeDate;

    txtForDate.text = [dateFormat stringFromDate:datePicker.date];

    [datePicker addTarget:self action:@selector(updateTextField:)
         forControlEvents:UIControlEventValueChanged];

    [txtForDate setInputView:datePicker];

// txtForDate is UITextField // txtForDate是UITextField

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

相关问题 UIPickerView作为UITextField的inputView - UIPickerView as inputView of UITextField 在选择时关闭作为 UITextField 输入视图的 UIPickerView - Close UIPickerView that is an UITextField inputview on selection 带有inputView的UITextField显示UIPickerView…如何在IOS 7中关闭半透明? - UITextField with inputView showing UIPickerView… how to turn off translucency in IOS 7? UITextField inputView未在iPhone 6中显示 - UITextField inputView not showing in iPhone 6 在iOS 8中将UIPickerview用作UITextfield的inputview时崩溃 - Crash when using UIPickerview as inputview for UITextfield in iOS 8 如何将UIDatePicker的背景颜色设置为UITextField inputView? - How to change background color of UIDatePicker set as UITextField inputView? 第一次点击时显示的键盘是UITextField.inputView = UIPickerView - Keyboard is showing on first tap for a UITextField.inputView = UIPickerView 工作几天后,UITextField.inputView突然不弹出UIPickerView - UITextField.inputView suddenly not popping up UIPickerView after working for days 当UITextField开始编辑时,UIPickerView不显示为输入视图 - UIPickerView isn't showing as inputview when UITextField begins to edit 在 iPhone 的 UIPickerView 中将 UITextField 的文本设置为默认值。? - Set UITextField's text as default value in UIPickerView in iPhone.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM