简体   繁体   English

通过点击UIBarButtonItem显示自定义inputView / Keyboard

[英]Show custom inputView/Keyboard by tapping on UIBarButtonItem

I'd like to show a custom date picker keyboard when tapping a Toolbar button (UIBarButtonItem). 我想在点击工具栏按钮(UIBarButtonItem)时显示自定义日期选择器键盘。

For showing custom keyboards using aa UIResponder 's inputView seems to be the right choice. 使用UIResponderinputView显示自定义键盘似乎是正确的选择。 Unfortunatelly the UIBarButtonItem isn't a subclass of UIResponder. 不幸的是,UIBarButtonItem不是UIResponder的子类。

How can I have a UIBarButtonItem becomeFirstResponder and carry an inputView to show such a keyboard? 我如何让UIBarButtonItem成为FirstResponder并携带一个inputView来显示这样的键盘?

Why do you want to show keyboard when user taps on UIBarButtonItem? 当用户点击UIBarButtonItem时,为什么要显示键盘? Suppose keyboard is shown on tap of UIBarButtonItem, what will happen when user types in something using keyboard? 假设在UIBarButtonItem的水龙头上显示了键盘,那么当用户使用键盘键入内容时会发生什么? Which control is expected to receive key press events from keyboard? 哪个控件应该从键盘接收按键事件?

Instead of inputView approach, you can follow another approach to show datePicker on button click 代替inputView方法,您可以按照另一种方法在单击按钮时显示datePicker

Let showDatePicker is the method called on button click then showDatePicker是按钮单击时调用的方法,然后

-(void)showDatePicker
    {

    UIDatePicker* picker = [[UIDatePicker alloc] init];
                        picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
                        picker.datePickerMode = UIDatePickerModeDate;

    [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
                        CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
                        picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
                        picker.backgroundColor = [UIColor blackColor];
                        [self.view addSubview:picker];
    }


-(void) dueDateChanged:(UIDatePicker *)sender {
    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    //self.myLabel.text = [dateFormatter stringFromDate:[dueDatePickerView date]];
    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
}

To remove the datePicker , you can use removeFromSuperView on either method called from another button click in your View or your custom toolbar done button. 要删除datePicker ,可以在从另一个按钮单击视图中调用的方法或自定义toolbar 完成按钮上使用removeFromSuperView

I've accomplished this by creating a UITextField 我已经通过创建一个UITextField完成了此任务

let myPicker = UIPickerView()
lazy var pickerInputField: UITextField = {
  let field = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
  self.view.addsubview(field)
  field.delegate = self
  return field
}()

func barButtonItemTouched(sender: Any) {

  // dismiss the picker if it's showing
  if pickerInputField.isFirstResponder {
    pickerInputField.resignFirstResponder()
  }

  pickerInputField.inputView = myPicker
  pickerInputField.becomeFirstResponder() 
}

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

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