keyboard not responding to resignFirstResponder

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 00:27:13

This is very easy to do, use your UITextFieldDelegate methods specifically UITextFieldShouldBeginEditing and return NO and execute the code to show the popover instead. This way the keyboard is never shown to begin with.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  [self.view endEditing:YES]; // added this in for case when keyboard was already on screen
  [self editStartDate:textField];
  return NO;
}

for it to work make sure you set the delegate of the textField to self (the view controller) and in your editStartDate method remove the resignFirstResponder call.

Try like this in your editStartDate: method

[self.startDateTextField resignFirstResponder];

EDIT: But instead of doing resign the keyboard when you click in textfield, you can make something like setInputView for Textfield to bring out the popViewController.

DatePickerVC *datePickerVC = [[DatePickerVC alloc] init];
datePickerVC.delegate = self;

self.popoverController = [[UIPopoverController alloc] initWithContentViewController:datePickerVC];
[self.popoverController setDelegate:self];

[self.popoverController presentPopoverFromRect:CGRectMake(0, 0, 5, 5) inView:textField permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

 self.startDateTextField = [[UITextField alloc] initWithFrame:CGRectMake(79, 148, 138, 27)];
[self.startDateTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.startDateTextField setDelegate:delegate];
self.startDateTextField.inputView = self.popoverController;

You appear to be resigning the first responder of the text field; however, this isn't necessarily the first responder, which may explain why calling it has no effect.

Instead, you should use the endEditing category to recurse through all children of your view to resign the first responder from whichever view it is attached to:

[self.view endEditing:YES];

In any case, as you never want to show the keyboard, you can simply implement UITextFieldDelegate to override the default behaviour.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // Your popover code.

    return NO;
}

UITextFieldDelegate Protocol Reference.

So you're trying to hide the keyboard immediately after the text field is selected and display something else?

There's two things I can think of:

  1. Give the text field some time to get it together before resigning the first responder: [textField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.5];

  2. Set the inputView property of the text field to nil or a UIView with a clear background color.

If inputView on the text field doesn't get you what you want, you can also simply put an invisible button on top of the UITextField and just show the popover from the button's action. To the user it will appear as if the text field brought up the popover. If at that point the keyboard is still there, call resignFirstResponder on all possible first responders (text fields etc.).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!