Stopping UISearchDisplayController to dismiss the searchbar keyboard - iphone

I have a UISearchBar which I am putting in UISearchDisplayController. Now, whenever I tap on the 'Search' button on the keyboard, it dismisses the keyboard. I am implementing the below method to stop searching whenever 'Search' button is tapped in which case I do not want to loose my keyboard also. Is there any way to instruct UISearchDisplayController to not dismiss the keyboard on tap on 'Search' button?
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
if ([searchBar.text length] < 3){
return;
}
else {
// Do searching here or other stuffs
}
}

// This method return NO to not resign first responder
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
So on your code it should be something like this to avoid the keyboard not to dismiss on search button tap only, but will dismiss on cancel, etc.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1 {
isSearchTap = YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
if(isSearchTap) {
return NO;
}
return YES;
}

Related

How to dismiss keyboard on second time selecting first textfield?

I'm trying to dismiss keyboard on textfield did begin editing.
First time selecting that textfield works fine, But in second time after resigning second textfield first textfield doesn't resign keyboard.
I tried hard but not get result
Can any one help me.
Thanks in advance.
You can hide keyboard by own method.
For Eg.
-(IBAction)hideKeyboard:(id)sender {
if (sender == txt1) {
[txt2 becomeFirstResponder];
}
else if (sender == txt2) {
[txt2 resignFirstResponder];
}
}
Bind both of textfields with 'Did End On Exit' method in xib.
After resigning first, it'll focus on 2nd. Then after resigning 2nd, keyboard will be dismissed. you can continue with many textfields in this manner.
It'll work definately.
Thanks.
Implement textField's delegate to all the textFields to your class.
And use this one:
- (BOOL) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
try this...
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == yourTextfield1) {
[textField resignFirstResponder];
}
}

UISearchBar cancel button Xcode 4.3?

How do I get the cancel button to only appear when the user starts entering text into the UISearchBar?
I tried this
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
self.searchBar.showsCancelButton = YES;
}
But it doesn't work, I don't think there's anything wrong with with setting it to "showsCancelButton" because even when I say
NSLog(#"Typing");
It won't print out to the screen. Is there another method in Xcode 4.3 that can do this?
Is there also a method that knows if the cancel button has been pressed?
Are you sure that the class is the delegate of searchBar? Try setting self.searchBar.delegate=self; in the viewDidLoad method.
Your method is not returning a BOOLean value, 'YES.' This should work:
// show search bar's cancel button when editing
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = YES;
return YES;
}
// hide search bar's cancel button when not editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
return YES;
}
Try this:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
aSearchBar.showsCancelButton = YES;
}

keyboard is not getting dismissed on UITextField

This may sound a newbie question, however I'm new to iOS dev.
Platform : iPad
I have a UITableView with UITextField, let say they are two.
When pressing on the first one virtual keyboard should appear, but when user tapps on the second UITextField the virtual keyboard should be hidden and data picker view should be displayed.
So here it is, how I did it.
-(void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag != PICKER_VIEW_TAG) {
...
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[self countriesPickerView];
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.tag == PICKER_VIEW_TAG) {
[textField resignFirstResponder];
} else {
...
}
return YES;
}
So now the question, when I click for the first time on the first UITextField it displays keyboard, but when I switch to second one it does not hide it. Why ? and how to solve this ?
UPDATE : The corresponding textField is not getting selected but i.e. the resign takes place, right ? but the keyboard is not hidden ... why this happens ?
The issue is with your textFieldShouldReturn. If you want it to complete the action, allow it to return while resigning the first responder.
[textField resignFirstResponder];
return YES;
Set the inputView property on UITextField to display views other than keyboard to receive input. In this case, you would set the text field's inputView to be an instance of UIDatePicker. The picker will be displayed with the same keyboard animation automatically and you get to delete a bunch of code. Win/win.

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)
}
}

How to cancel the keyboard in UISearchbar?

I'm using UISearchBar in my application and it serves as both an edit field as well as a search. So when I want to disappear the keyboard I have to use cancel button in UISearchBar but I can't have that cancel button on screen, so how could T make keyboard disappear when not used without using cancel button. Please help me as i'm new to iPhone application development.
Thanks in advance!
Use this:
[UISearchBar resignFirstResponder];
Just replace the word UISearchBar with the name of the object you have created.
Are you looking for ways you can dismiss the keyboard or how to actually do that programmatically? If programmatically, then [UISearchBar resignFirstResponder]. If you are looking for a possible way for the user to achieve that you can either make the return button on the keyboard resign its first responder status when pressed, or attach a UIGestureRecognizer to your view and set it up so that when the user clicks outside the keyboard, this keyboard goes away.
simply all you need to do is to get UITextfield control from the UISearchbar and then set UITextfield's delegate to whatever delegate that will perform -(void) textFieldShouldReturn:(UITextField *)textField
-(void)viewDidLoad{
UIView * subView;
NSArray * subViews = [searchbar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return TRUE;
}