Reputation: 478
I have a UISearchBar
that, even though it is the first responder, the keyboard is not shown. I can start typing in the search bar using the laptop's keyboard, and I can see what I type inside the search bar, but the keyboard in the Simulator and on the actual iPad does not appear. It worked fine under iOS 3.2, but stopped working after updating to iOS 4.2 this morning.
Below is the relevant code:
// Text Field that when touched will fire a search view that contains the search bar - (void)textFieldDidBeginEditing:(UITextField *)textField { [textField resignFirstResponder]; UIViewController *detailViewController = nil; ImplementedSearchViewController *searchController = [[ImplementedSearchViewController alloc] initWithNibName:@"ImplementedSearchView" bundle:nil]; ... detailViewController = searchController; MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; UINavigationController *nav = (UINavigationController *) [delegate.splitViewController.viewControllers objectAtIndex: 0]; NSArray *viewControllers = [NSArray arrayWithObjects:nav, detailViewController, nil]; self.splitViewController.viewControllers = viewControllers; HomeViewController *hHomeController = [nav.viewControllers objectAtIndex:0]; // Dismiss the popover if it's present. if (homeController.popoverController != nil) { [homeController.popoverController dismissPopoverAnimated:YES]; } // Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created). if (homeController.rootPopoverButtonItem != nil) { [detailViewController showRootPopoverButtonItem:homeController.rootPopoverButtonItem]; } [detailViewController release]; }
// Inside the viewDidLoad of the search view - (void)viewDidLoad { self.table.frame = CGRectMake(table.frame.origin.x, table.frame.origin.y, table.frame.size.width, 680); self.table.backgroundColor = [UIColor colorWithRed:239/255.0 green:244/255.0 blue:255/255.0 alpha:1.0]; // searchBar is a UISearchBar [self.searchBar becomeFirstResponder]; }
The ui search bar in viewDidLoad
is not nil
.
Any thoughts on this?
Thanks, Mihai
Upvotes: 1
Views: 2672
Reputation: 350
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
This answer from Mann(https://stackoverflow.com/users/871079/mann) worked for me. Thank you very much.
Upvotes: 0
Reputation: 5507
I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.
Keyboard was not showing. So for that first resign textField as firstResponder. i.e.
[textField resignFirstResponder];
Then call method with delay
[self performSelector:@selector(callSearchBar) withObject:NULL afterDelay:0.2];
Where
-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
}
It works
Upvotes: 1
Reputation: 11
I can verify, that resigning the first responder in favor of the searchbar solves the problem on an iPad under iOS 4.3. In my case I had an UIViewController responding to UIEventSubtypeMotionShake. You had to explicitely call resignFirstResponder before(!) removing thie UIViewController from the view hierarchy to make it work.
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake){
...
}
}
...
[someViewController becomeFirstResponder];
...
[someViewController resignFirstResponder];
[[someViewController view ] removeFromSuperview];
... (in my case I added a hierarchy including an UITabBarController, with a Tab including a UINavigationController covering the Searchcontroller to the window instead)
[searchViewController. searchbar becomeFirstResponder];
No clue, why this was not necessary on iPhone.
Upvotes: 0
Reputation: 478
I am using the same ui search bar in different places. It works in all but two. I believe that the problem has to do with the text field not resigning as first responder (although I called resignFirstResponder on that text field). In one of the places that it works, I'm also using a text field to trigger the search bar, but I'm using a selector instead of a text field delegate method (such as textFieldDidBeginEditing). If I switch from using a selector to using the delegate method, it stops working and I get the same issue (keyboard not appearing). I thought that, for the two cases where it doesn't work, the solution would be to switch from using the delegate method to using a selector, but it didn't. What I ended up doing was switching from using a text field to using a button. In a way, it doesn't make any sense using a text field if you are not going to write anything in it (I was only using it to trigger the search bar).
Upvotes: 0