Why doesn't the UISearchBar get Focus even after it is configured as the first responder?

Why doesn't the UISearchBar get focus and the keyboard doesn't appear when the UIViewController is switched to the navigation controller a second time? It seems that not a single element as a whole has focus.

MY USE CASE : I have 2 view controllers: A and B. Transition - A-> B. A, B are popped onto the navigation controller.

  • When I show B , the first time the focus is set to the search bar and the keyboard appears. OK
  • Then I return to A. And again from A-> B. Now for the second time it is out of focus and the keyboard does not appear . It is not right.

MY CODE : An IBOutlet connection to the search panel was created on the B UIViewController:

@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar; 

Also installed mySearchBar delegate. I focused on the search bar in the B UIViewController in the viewDidAppear method:

 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self performSelector:@selector(setCorrectFocus) withObject:NULL afterDelay:0.2]; } -(void) setCorrectFocus { [self.mySearchBar becomeFirstResponder]; } 

Transitions between controllers are performed manually in the code as follows:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil]; AController *a = [storyboard instantiateViewControllerWithIdentifier:@"A"]; [self.navigationController pushViewController:a animated:NO]; 
+6
source share
5 answers

After a long search, I found an error. Somewhere in the storyboard, the old connection for the UISearchBar named mySearchbar remained. Another thing that I had to fix was that the method

 - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; 

NO returned every time I return to the previous controller. But he must return YES so that next time he can set focus on the search bar.

+2
source

try it

 -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self setCorrectFocus]; } -(void)setCorrectFocus { [self.mySearchBar becomeFirstResponder]; } 
+4
source

I struggled with this - had a UISearchBar inside the UITextView keyboard accessory, and even after [self.searchBar becomeFirstResponder] was called, text input was still received by the text view.

Here is what helped:

 [self.searchBar.window makeKeyAndVisible]; 

Apparently, a keyboard accessory is part of a different UIWindow than a UITextField.

Note. To continue entering the text view, you should call something like [self.textView.window makeKeyAndVisible]; ...

+2
source

Have you tried adding self.mySearchBar.delegate = self;

0
source

You must use a strong property.

 @property (strong, nonatomic) IBOutlet UISearchBar *mySearchBar; 

Always set the property for IBOutlet in the property. When you push B from the navigationController, mySearchBar is freed, so you return to B, [self.mySearchBar becomes FirstResponder] does not work.

-1
source

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


All Articles