Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus on the UISearchBar but the keyboard not appear

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.

like image 428
Lorenzo Avatar asked Mar 29 '11 14:03

Lorenzo


2 Answers

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

like image 193
Mann Avatar answered Sep 16 '22 21:09

Mann


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];

}

like image 38
Andy A Avatar answered Sep 17 '22 21:09

Andy A