Keyboard not responding to resignFirstResponder

Instead of showing the keyboard, I want to display a pop-up view when selecting textField (my code is below). If the keyboard does not appear, everything works fine. However, if the keyboard is shown and then a text field is selected, the keyboard is not discarded, somewhere the first responders should get lost, but I do not know where. Anyone have a solution?

My text box:

self.startDateTextField = [[UITextField alloc] initWithFrame:CGRectMake(79, 148, 138, 27)]; [self.startDateTextField setBorderStyle:UITextBorderStyleRoundedRect]; [self.startDateTextField setDelegate:delegate]; [self.startDateTextField addTarget:delegate action:@selector(editStartDate:) forControlEvents:UIControlEventEditingDidBegin]; [popoverWrapper addSubview:self.startDateTextField]; 

and in editStartDate: I have:

 -(void)editStartDate:(UITextField *)textField { [textField resignFirstResponder]; 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]; } 
+2
source share
5 answers

This is very easy to do, use your UITextFieldDelegate methods specifically with UITextFieldShouldBeginEditing and return NO and execute the code to display a popover instead. Thus, the keyboard is never displayed from the beginning.

 - (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 textField delegate for yourself (the view controller) and in your editStartDate method, remove the resignFirstResponder call.

+8
source

It seems you are leaving the first responder of the text field; however, this is not necessarily the first responder to explain why his challenge has no effect.

Instead, you should use the endEditing category to endEditing all the children of your view in order to undo the first responder from the view to which it is attached:

 [self.view endEditing:YES]; 

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

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { // Your popover code. return NO; } 

Link to the UITextFieldDelegate protocol.

+1
source

Try using this editStartDate: method editStartDate:

 [self.startDateTextField resignFirstResponder]; 

EDIT: But instead of canceling the keyboard when you press the text field, you can do something like setInputView for the text field to display 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; 
0
source

So, are you trying to hide the keyboard immediately after selecting the text field and display something else?

I can think of two things:

  • Give the text field some time to assemble it before leaving the first responder: [textField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.5];

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

0
source

If the inputView in the text box does not get you what you want, you can also just put an invisible button on top of the UITextField and just show the popover from the button action. For the user, it will look as if the text field has raised a popover. If at this point the keyboard still exists, call resignFirstResponder for all possible first responders (text fields, etc.).

-1
source

Source: https://habr.com/ru/post/927134/


All Articles