iPhone SDK: UISearchBar: searchBarTextDidEndEditing not firing - iphone

I'm implementing a search bar on my table, which should be pretty straight forward. I've got these:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
NSLog(#"searchBarTextDidBeginEditing");
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"The search text is: %#", searchText);
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)theSearchBar {
NSLog(#"searchBarTextDidEndEditing");
[theSearchBar resignFirstResponder];
}
And searchBarTextDidBeginEditing fires, and I get that message in my log, but when I tap outside the search bar, above the keyboard, I don't get the searchBarTextDidEndEditing event so I can't make the keyboard disappear – the message doesn't even appear in the log.
The textDidChange is working, so it's just searchBarTextDidBeginEditing that isn't.
Any ideas? Thanks!!

Even i faced the same problem.
Please find with the solution below
Implement Below methods
1.searchBarTextDidEndEditing
2.searchBarSearchButtonClicked
and make sure you [UISearchchbar resignfirstresponder] in the second method mentioned above

Once I implemented searchBarSearchButtonClicked that solved it for me.

The method
(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
is only called when [searchBar resignFirstResponder] is called.
The best place to call [searchBar resignFirstResponder] is in the method
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
In swift:
(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

Related

Weird behavior of searchbar: keyboard getting dismissed after typing first letter

After typing first letter in search bar keyboard getting dismissed .
Not understanding why it is happning. Already used searchbar so many times.
I have used
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
and
-(void)searchBarSearchButtonClicked:(UISearchBar *)sBar
these delegate methods.
Nothing else done with resigning keyboard but still after typing first letter in searchbar keyboard getting dismissed.
Please any one gone through same issue/ have any clue help me.
here is code
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText length]>0)
{ //searching code goes here
}
else if ([searchText length]==0)
{
// code to reload table
}
}
Thanks in advance.

iPhone UISearchBar takes two clicks to be selected

I have a UISearchBar in my app, and I'm running the following code with it:
- (void)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
So when the user clicks the search bar, a cancel button pops up next to it.
This, unfortunately, makes it so the text box isn't selected, and the keyboard doesn't pop up. You have to click the search bar a second time to get those things to happen.
How would I fix this?
Thanks.
The method should be like this:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
return YES;
}
The key element is returning YES in this method to indicate that it should in fact begin editing.
Documentation here: http://developer.apple.com/library/ios/documentation/uikit/reference/UISearchBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UISearchBarDelegate/searchBarShouldBeginEditing:
could you change your code as below
[searchBar setShowsCancelButton:NO animated:YES];
I think your were passing YES in the above method.

SearchBar not displaying results until cancel button clicked

I have implemented a UITableView with search bar (and search display) - all works fine, but the table results do not get updated until the search bar cancel button is tapped.
Delegate methods:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// asynchronous request with [self.tableView reloadData] in the connectionDidFinishLoading
[self getProductData:searchBar.text];
[searchBar resignFirstResponder];
[self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}
Do I need to call a delegate method after receiving the data from the server? Or should I make the request synchronous?
Thanks
edit: I tried with a synchronous request and it still does not work!
Resolved this issue by adding this code to the end of my getProductData method:
[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
I think you have to implement:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
and return YES
See the TableSearch sample code
Please try something like this:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self getProductData:searchText];
[self.tableView reloadData];
}

UISearchBar and resignFirstResponder

I have a very basic UITableView with an attached UISearchBar, and here's the flow of what happens
UITableView is empty, user taps UISearchBar, and brings up keyboard.
Once the user taps the Search button
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; //move the keyboard out of the way
//Code....
}
Works just fine, and moves the keyboard out of the way, and populates the UITableView.
The problem is any subsequent search attempts.
The same steps as before occur, however the keyboard is never dismissed. I have a feeling something else is becoming the responder, I just need a little clarity to understand what is actually occurring.
Without seeing your code it is difficult to guess. However, if you include:
[self.view endEditing:YES];
all views will resign first responder.
Not perfect but did work for my case. Will not work without dispatch_after
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (!searchText.length) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[searchBar resignFirstResponder];
});
}
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self performSearchWithString:searchBar.text];
[searchBar resignFirstResponder];
}

Search Display to not show results until search bar is tapped

I think I may be missing something obvious:
Is there a simple way to modify the UISearchBar or UISearchDisplayController so that it doesn't reload the view after each letter typed but only after the Search button is hit?
I figured it out. Most of the examples I found search the text here:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
but I just didn't implement that method and instead just wrote my own search function which I called here:
// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self fetchTheSearchString:searchBar.text];
[searchBar resignFirstResponder];
[self.tableView reloadData];
}