UITextField starts an unwanted session when the keyboard is rejected

I am using a Storyboard with this project and have a UITextField inside the view. The user can enter a search query, click "return", and "sege" - on resultViewController, as intended.

The problem I am facing is that if for any reason the keyboard is rejected, segue happens automatically. For example, if a user presses the iPad drop keyboard key, segue occurs without a search query ... or if the user goes beyond the UITextField, the keyboard crashes (as intended), but segue also occurs (not intended).

Here are the methods I'm using (the UITextField delegate is set in the storyboard); In addition, I put the messages "resignFirstResponder" and "endEditing: YES" in several places when I was trying to find a solution. Sorry for the mess:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField.text isEqualToString:@""]) { [textField resignFirstResponder]; return NO; } self.clueString = textField.text; [textField resignFirstResponder]; return YES; } - (BOOL) textFieldShouldEndEditing:(UITextField *)textField { [self.view resignFirstResponder]; return YES; } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } 
+4
source share
2 answers

Well, I feel stupid, but I will answer my question if anyone else has this problem.

To control when segue should or should not appear, I needed to implement the following method:

 -(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { if ([self.searchField.text isEqualToString:@""]) { return NO; } return YES; } 
+5
source

Have you tried setting this method to always return no? You may need to check whether to search or not. (Like your method above)

 - (BOOL) textFieldShouldEndEditing:(UITextField *)textField { [self.view resignFirstResponder]; return YES; } 
0
source

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


All Articles