UISearchBar and resignFirstResponder - iphone

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

Related

How i dismiss keyboard on tapping search button on keyboard regarding UISearchBar?

I m developing an application in which i adding one control UISearchBar. When i started editing text in UIsearchBar then keypad is animated on the screen. After i completed my complete editing or canceling all text then i will stuck on point of dismissing keypad.
How i dismiss keyboard on tapping search key form UIKeypad?
Also same question for UITextField and UITextView?
Thanks
Make sure the view controller which has the SearchBar implements the SearchBarDelegate and you set the searchBar.delegate to self:
#interface AddressSearchViewController : UIViewController <UISearchBarDelegate>
then implement the following method:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
This will make the keyboard disappear when you tap the search button on the keyboard or the search bar
For the search bar:
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
And same thing for the text field:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
Use this code in ViewDidLoad
-(void) ViewDidLoad
{
[super ........];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
}
- (void) hideKeyboard
{
[texfieldname1 resignFirstResponder];
[texfieldname2 resignFirstResponder];
}

Search Bar Cancel Button is Not Working

My App is having a search bar for searching records from the table view,which is populated by sqlite DB.
My problem is that when the view opens the "cancel" button is not enabled and also I cant touch on that, just like a image only.It is there but no action is with that.
when we click on that search bar text the cancel button will be changed to "done" it is enabled one.
so here is my code
this is my search bar view,see that cancel button.It is not enabled
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
//[newSearchBar setShowsCancelButton:YES animated:YES];
newSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
NSLog(#"search begin edit") ;
//searchString = searchBar.text;
//NSLog(#"print did edit searchstring : %#", searchString) ;
for(UIView *view in [searchBar subviews])
{
//shareItemId =newSearchBar.text;
if([view isKindOfClass:[NSClassFromString(#"UINavigationButton") class]]) {
[(UIBarItem *)view setTitle:#"Done"];
}
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(#"searchBarTextDidEndEditing:");
[searchBar resignFirstResponder];
//[self dismissModalViewControllerAnimated:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(#"searchBarSearchButtonClicked");
searchString = searchBar.text;
NSLog(#"search %#", searchBar.text);
[newSearchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
//[self dismissModalViewControllerAnimated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(#" searchBarCancelButtonClicked");
[searchBar resignFirstResponder];
shareItemName =newSearchBar.text;
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
NSLog(#"searchBarShouldBeginEditing");
[newSearchBar setShowsCancelButton:YES animated:YES];
return YES;
}
These are my delegates for that
Please check my code and give me the answer. I need to enable the "Cancel" button when the view is loaded and it action will be go back to previous view
I need like this
Or else how can I add a another cancel button on exciting cancel button.so that I can enable that.please give me all the details
You need to set the UISearchDisplayController to be ACTIVE, like this:
[mySearchDisplayController setActive:YES animated:YES];
or more simply:
mySearchDisplayController.active = YES;
My guess is that Apple made the UISearchBar in a way that the cancel button is disabled if the search text field is empty or not first responder.
This is make sense because you should not use the "Cancel" button to other purpose than actually canceling the search. and since there is no search to cancel - the button is disabled.
If you still want that the button will be active immediately when the view is presented, you can call at viewWillAppear: to [mySearchBar becomeFirstResponder];
This will cause to the keyboard to appear and the button will be enabled.
And then if the user hit cancel you can intercept it to go back to the previous view. (I'm not sure if apple will like this behavior).
Sample code:
-(void) viewWillAppear : (BOOL) animated
{
[super viewWillAppear:animated];
// Make keyboard pop and enable the "Cancel" button.
[self.mySearchBar becomeFirstResponder];
}
Here's what I did to always enable the cancel button, even when the search field is not first responder.
I'm calling this method whenever I call resignFirstResponder on the search field
- (void)enableCancelButton {
for (UIView *view in self.searchBar.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[(UIButton *)view setEnabled:YES];
}
}
}
This works, but I'm not sure whether it will pass App Store verification yet, so use it at your own risk. Also, this probably only works if the cancel button is the only button you are using with the search field.
This works to reenable the cancel button as of iOS 8:
private func enableButtonsInSubviews(view: UIView) {
if let view = view as? UIButton {
view.enabled = true
}
for subview in view.subviews {
enableButtonsInSubviews(subview)
}
}

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.

Strange UISearchBar cancel button behaviour in iphone (Working fine in Simulator)

I am showing some Lists in UITableView. For sorting purpose i have used UISearchBar. I am getting some strange behaviors of Cancel button
Steps to reproduce
Search for something
Notice that while you are searching, there is a 'Cancel' button that will get rid of the onscreen keyboard.
Now tap into detail of one of the search results
Then go back
The search results are still there, and the search bar is there, but the 'Cancel' button is missing.
So there is no way to remove the onscreen keyboard without closing the application and re-opening.
But these scenario occurring only in iphone not in simulator. I am able to see Cancel button when i go back to first screen.
I have used these delegates:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = nil;
[searchBar resignFirstResponder];
isSearch = NO;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = NO;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = YES;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:YES animated:YES];
isSearch = YES;
return YES;
}
Please help
Try this post.
I would suggest you use UISearchDisplayController - a helpful tutorial?

iPhone SDK: UISearchBar: searchBarTextDidEndEditing not firing

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;