繁体   English   中英

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

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

我正在尝试将UIPickerView作为inputView打开到UITextfield

这是我在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;

第一次工作正常,但是当我从UIPickerView选择任何值时,它正在隐藏并且我的UITextfield是可编辑的,而UIPickerView当时没有显示。 我究竟做错了什么?

我建议您将代码移至viewDidLoad: 因为textFieldDidBeginEditing:在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是UITextField

暂无
暂无

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

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