11

I've read so many solution on how can i focus a searchbar to make keyboard appear when i open my search view, and all of that are like this

[searchBar becomeFirstResponder];
mine is 
[self.searchDisplayController.searchBar becomeFirstResponder];
but I tried both.

Now, I tried this, and I also added a

[self.searchDisplayController setActive:YES];

because I'm using a SearchDisplayController, but so far the best result i can have is to have the cursor on the searchbar, the uitableview with an overlay on it, but still no keyboard.

If I run the simulator I can type on the searchbar with my computer's keyboard, but on an iPhone I can't do anything.

If you want to give a look to my code: http://nopaste.info/39fcda4771.html the focus should be executed in viewDidLoad method

Thanks again.

Lorenzo
  • 133
  • 1
  • 2
  • 6
  • same issue I am facing, my searchBar is in focus. On simulation cursor blinking, but keyboard not showing. Have you found solution – Amit Battan Oct 29 '13 at 12:52

4 Answers4

8

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

Mann
  • 5,477
  • 6
  • 45
  • 57
  • This finally worked for me! Have an upvote. Any idea why you would have to do this `[self.searchDisplayController.searchBar becomeFirstResponder];` instead of `[self.searchBar becomeFirstResponder];`? – scottmrogowski Jun 24 '13 at 03:49
  • This works fine, but I'd recommend simply doing `[self callSearchBar]` inside the `viewDidAppear:` method. We don't know reliably how much time the controller transition might take, so you might end up with a ugly effect and/or unexpected tableview artifacts if your delay is too short to focus the `searchBar`. +1 nevertheless :-) – jweyrich Sep 27 '13 at 03:34
  • @scottmrogowski You have to use `self.searchDisplayController.searchBar` because you have probably got the `UISearchDisplayController` connected to your `UITableViewController`, so you haven't had a searchBar variable into your `UITableViewController" class, but you can get this reference through the `UISearchDisplayController` variable. – ricky.tribbia Dec 17 '13 at 14:13
5

Use the UISearchBarDelegate and declare the searchBar in the header file like this ...

@interface mySearchScreen : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {

UITableView *myTableView;   
NSMutableArray *tableData;
UISearchBar *sBar;//search bar

and declare your search bar in loadView

- (void)loadView {  
[super loadView];   
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,30)];   
sBar.delegate = self;   
[self.view addSubview:sBar];    
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 31, 300, 400)];   
myTableView.delegate = self;    
myTableView.dataSource = self;  
[self.view addSubview:myTableView];

tableData = [[NSMutableArray alloc]init];

}

and then use becomeFirstResponder on your search bar in viewDidAppear

- (void)viewDidAppear:(BOOL)animated {
//ensure the keyboard comes up from the start
[sBar becomeFirstResponder];    
[super viewDidAppear:animated];

}

Andy A
  • 4,191
  • 7
  • 38
  • 56
3

The sequence matter

[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];

If no luck then check if delegate of the search display controller is linked. Also worth checking the tint colour of the search bar.

Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35
3

On the simulator, make sure that you click on Toggle Software Keyboard to use the keyboard of the simulator as you can see in the image.

enter image description here

Sawsan
  • 1,096
  • 10
  • 20