Hiding keyboard iphone sdk? - uitableview

I have a UISearchBar. I want the the keyboard to go away as soon as user hits search...i did try resignFirstResponder but that didn't work. any help would be appreciated
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Songs", #"Search for songs");
NSMutableArray *array = [[NSArray alloc]initWithObjects: #"Book_1", #"Book 2", #"Book _ 4", nil];
self.booksArray = array;
[array release];
search.delegate=self;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
thanks
TC

Please make sure of following things.(I hope you are talking about search Button of keyboard.)
You have connected your IBOutlet of searchBar with its variable search.
You are not de-referring search variable anywhere in your code(reassigning it).
Your - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar method is in same viewcontroller as searchBar is in.
Please NSLog(#"Search Bar Instace : %#",search); in viewDidLoad method and searchBarSearchButtonClicked and verify that both instances are same if not then you are search is getting reassigned somewhere in code.
Just after [searchBar resignFirstResponder]; NSLog(#"isFirstResponder : %d",[searchbar isFirstResponder]); and NSLog(#"Next Responder : %#",[searchBar nextResponder]);
Is - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
method is being called when you tap search Button of keyboard? Make sure there are no keyboard responder(like textField,textView or other searchBar) is added behind your searchBar may be unintentionally. Please check it through xib also.
Thanks,

It should work by implementing the respond methods at the delegate object:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
Reference: http://www.iphonedevsdk.com/forum/iphone-sdk-development/7148-problem-uisearchbar-navigation-controller.html#post59467

Related

keep keyboard when searchDisplayControllerWillEndSearch is invoked

With help from other posts on this site, I have managed to open up search display controller with keyboard when a button is tapped. I can see the underlying UIView is dimmed too.
What I now want to do is, not have the keyboard dismissed when the user taps outside the search bar. Currently, the keyboard gets dismissed and searchDisplayControllerWillEndSearch is invoked.
I have tried combinations of the following but none worked.
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar setShowsCancelButton:YES animated:YES];
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder]
Can anyone please suggest any ideas?
I haven't been able to try this, but I think it should work
Declare something as searchbar delegate;
#interface YourViewController : UIViewController <UISearchBarDelegate>
Then, somewhere in YourViewController.m, set:
- (BOOL) searchBarShouldEndEditing:(UISearchBar *)searchBar {
return NO;
}
You can set it to return something dynamic, for instance if we only wanted to allow it to stop editing if there isn't any text, we could do this.
- (BOOL) searchBarShouldEndEditing:(UISearchBar *)searchBar {
return (searchbar.text.length > 0) ? YES : NO;
}
This way, if the user has started typing, or any text is in the search bar, it won't resign.
Let me know if this works.

searchBarTextDidBeginEditing delegate method called two times

I'm debugging my code, where UISearchBar's delegate method searchBarTextDidBeginEditing: is called exactly twice every time I tap on the search bar (which is in the navigation bar).
The odd thing is that only this delegate method is called twice. The others are called only once within the whole proccess, which is the correct behavior.
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// called twice every time
[searchBar setShowsCancelButton:YES animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
// called only once
}
Any idea what could be wrong?
The UISeachrBar is set up in Storyboard, with properly connected outlets, although it's not added to any view, and in the particular view controller's viewDidLoad is the following line that adds the search bar to the navigation bar:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
I'm using Xcode 5.0.1 and running the code in iOS 7.0.3 Simulator.
I was experiencing the same issue, and dug in a bit deeper.
In my case, I had a subclass of UISearchDisplayController which functioned as UISearchDisplayDelegate for itself, and UISearchBarDelegate for its UISearchBar.
As it turns out, the problem is that UISearchDisplayController implements the following methods that collide with theUISearchBarDelegate` protocol:
- (void)searchBar:(id)arg1 textDidChange:(id)arg2;
- (void)searchBarCancelButtonClicked:(id)arg1;
- (void)searchBarResultsListButtonClicked:(id)arg1;
- (void)searchBarSearchButtonClicked:(id)arg1;
- (void)searchBarTextDidBeginEditing:(id)arg1;
This means if you make a UISearchDisplayController the delegate of its own UISearchBar, those methods will be called twice.
I found that if you unset the searchBar delegate and only keep the searchDisplayController delegate the method is not called at all any more. So the only workaround i could come up with is putting this in the beginning of your searchBarTextDidBeginEditing and searchBarTextDidEndEditing.
static NSDate *lastInvocation;
if ([[NSDate date] timeIntervalSinceDate:lastInvocation] < 0.1f) {
lastInvocation = [NSDate date];
return;
} else {
lastInvocation = [NSDate date];
}
I found this solution:
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
But funny thing is, I removed it after some two rounds of testing, the code is just called once, not twice
I had problem with searchBarSearchButtonClicked: method been called twice.
Problem was solved by calling [searchBar resignFirstResponder];
#pragma mark - UISearchViewDelegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// Do things
[searchBar resignFirstResponder];
}

Setting text to the UISearchBar when its firstresponder with controller change works weird

In my app i have UITextField on view of VC1(UIViewController). When i change text in textfield i call pushing of another controller VC2 with UISearchBar on its view. After pushing im assigning UISearchBar text to the textfield text from VC1.
On xib my textfield already have some text "Test string".
When I'm append VC1 textfield with any char - VC2 pushing and text on searchbar is normal.
But when I'm press backspace key on iPhone keyboard - VC2 pushed and text on searchfield start deleting char by char, while whole string not been empted. It happend because delegate method calls recursively.
How to fix that behaviour of UISearchBar? I mush to have searchbar active with keyboard opened when VC2 appears! It's main condition. Sure, if i'll remove [self.searchBar becomeFirstResponder] all will works fine.
Some code here:
#implementation ViewController1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
ViewController2 * vc2 = [ViewController2 new];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 loadText: resultString];
return NO;
}
#end
#implementation ViewController2
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.searchBar becomeFirstResponder];
}
- (void) loadText: (NSString *) text
{
self.searchBar.text = text;
}
#end
Sample source code of the problem: http://yadi.sk/d/NJmTLot73_vrE
I've gone through the code and for some reason the delete/backspace key is getting called repeatedly by the UIKeyboard's Accessibility function. I haven't been able to find a reason yet but one workaround is to put the [self.searchBar becomeFirstResponder]; line into viewDidAppear instead of viewWillAppear - is that an acceptable workaround? The keyboard animation is slightly different but I'm not sure how sensitive your needs are to that.
I've been experiencing strange UISearchBar animations on iOS 7. Solved the problem by putting my becomeFirstResponder call in viewDidAppear with a delay of 0.1.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.searchBar performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.1];
}
You have a recursive call in your code.
self.searchBar.text = text;
calls to
textField:shouldChangeCharactersInRange:
which in turn calls to
loadText:
What you can do is remove the delegate from the searchBar uitextfield, set the text and then return the delegate. Something like this:
- (void) loadText: (NSString *) text
{
self.searchBar.delegate = nil;
self.searchBar.text = text;
self.searchBar.delegate = vc1;
}

UISearch Bar Not Being Updated By Keyboard Input

I have a ViewController with a subview that contains a UISearchBar. When I click on the search bar the keyboard appears and I am able to close it using the cancel button. The problem seems to be that it isn't accepting any of the input when a user clicks on the keyboard buttons.
#pragma mark -
#pragma mark SEARCH METHODS
#pragma mark -
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
self.mainSearchBar.showsCancelButton = YES;
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.mainSearchBar resignFirstResponder];
self.mainSearchBar.showsCancelButton = NO;
self.mainSearchBar.text = #"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self.mainSearchBar resignFirstResponder];
}
Has anyone had this issue before or know how to fix it?
** UPDATE *****
It turns out the Siri will work when trying to edit the UISearchBar. Still no luck with the keyboard input though. I think it has something to do with the fact that the UISearchBar is situated on a UIView that is then added as a subview. Not sure how to fix it though.
** UPDATE 2 *****
Moved the search box to the main UIView and I am still getting the same error. I am wondering if it is now related to the Navigation Controller.
Make sure that the app delegate has a [self.window makeKeyAndVisible]. Hopefully it will solve your problem.
Check whether you have added a UISearchBarDelegate in your viewController's .h file.
Try this code also:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(#"%#",searchText);
}
This has to be related to how the SearchBar is rendered in relation to the ViewController's view. Why are you making the SearchBar a subview of another view rather than a subview of the ViewController's view?
Problem solved, the answer was found on a previous thread Keyboard and cursor show, but I can't type inside UITextFields and UITextViews
It turns out that in IOS6 the Visible at Launch check needs to be clicked on the options.

UIScrollview frozen when search bar in use

I have a Controller with a search bar at top, and when the user types, autocompleted search results are shown below it, in a UIScrollView.
Problem:
The results show up, but the user can't scroll those results. The scrollview is frozen. The only way to scroll is to push 'Cancel' in the Search bar. Tapping "Search" hides the keyboard, but even then the scrollview is frozen.
Desired:
As the user is typing, search results are being autocompleted. At any given time, the user can scroll through those results. They should not have to hit Cancel in order to scroll.
There are more results than will fit on the screen so this isn't an issue of content being only slightly larger than the screen.
Nothing special in CancelButtonClicked or SearchButtonClicked:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text = #"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[NSThread detachNewThreadSelector:#selector(fetchSearchResult:) toTarget:self withObject:searchBar.text];
}
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
if([searchBar.text length] >=3){
[mySpinner startAnimating];
[NSThread detachNewThreadSelector:#selector(fetchSearchResult:) toTarget:self withObject:searchBar.text];
}
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchDisplayController.searchResultsTableView.hidden = YES;
// This occurs when user starts entering text
// We want to keep the background area dark
}
I had possibly the same issue: UISearchBar was nested into UIScrollView, so when search results was shown- It was impossible to scroll parent view.
..I was puzzled but I learned (UISearchDisplayCtrl private api) that UISearchDisplayController locks all parent scroll views (bitch).
So U should add category UISearchDisplayController+Custom:
#implementation UISearchDisplayController (Custom)
- (void)_disableParentScrollViews {
}
- (void)_enableParentScrollViews {
}
#end

Resources