How to dismiss keyboard when the UISearch Bar 'Search" button is pressed? - objective-c

I've implemented a UISearch bar with the searching functionality set up, however when I press the "Search" button on the keyboard that shows up nothing happens. How can I get the keyboard to hide when the "Search" button is pressed while keeping the text in the search bar intact (To keep the search results present)?

From Text, Web and Editing Programming Guide for iOS:
To dismiss the keyboard, you call the resignFirstResponder method of the text-based view that is currently the first responder.
So you should do this in your UISearchBarDelegate:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}

Swift 4
Make sure you have UISearchBarDelegate defined in your UIViewController
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}

Related

Hide the keyboard for UITextField in the same UIViewController with UISearchbarDisplayController

How can I hide the keyboard only for specific UITextFields?
I have a lot of UITextField in a View Controller, which has also a SearchbarDisplayController.
I tried to use this but it blocks the UISearchbarDisplayController's job.
Thank you.
For more detail:
For example I have 3 UITextFields and a searchbar display controller. If there is only UITextFields , while tapping out ofthe keyboard, the keyboard successfully disappear. But when I touch the searchbar, you know the keyboard is open and I enter some text to searchbar. So it gives me some results in searchresultstable of the searchbar. But I can't select any row from searchresultstable. Because the disapper keyboard blocks it.
it seems to me that in this tutorial this method has not been implemented, try to add it
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
}

iOS7:editable textfield cells in tableview is not working properly

Editable textfield cells Tableview is causing problem on keyboard tab button everytime it is calling textfieldshouldbeginediting even if i am in first textfield it is not going to nextfield.
It is going to last textfield and if popover is availabe it will crash.How can i fix this so that if enter tab then it has to resign current responder in textfielddidendediting and it should not go to textfieldshouldbegin editing.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
PickerViewController *selectOperatorController;
NSLog(#"tag %d",textField.tag);
return NO;
}
I also declared textfield delegates like didendediting and shouldendediting
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
activeField = nil;
if (self.chooseOperatorController) {
[self.chooseOperatorController dismissPopoverAnimated:YES];
}
return YES;
}
This is not as issue in iOS 6.But it is in iOS 7.
textfieldshouldbegin editing will not allow desktop keyboard tab button input,it cannot judge to move between the textfields.If we add textfielddidbeginediting we can move the controls,eventhough we have two methods we can move by using keyboard tab.So textfielddidbeginediting is mandatory if we want to move bewtween available textfields.

On clicking a tab need to answer an alert before moving into the tab clicked - iPad programming

There are many tabs in my screen,I want to give an alert box which says "Do you want to save the changes?" if user changes anything in the page, without clicking on the save button provided in the page,he is clicking on diff tab.
I'm able to get the alert view but the tab click moves the screen to the tab which was clicked. The screen should not change until the alert view is answered.
Can anyone let me know how to suppress the screen change until the alert view is answered ?
This doesn't directly answer your question, but: what you're trying to do sounds like bad UI design. (In general, if it feels like you are fighting against UIKit, you're probably doing it the wrong.)
In this case: if you really want to ensure that a user taps a Save button before moving to a different screen, you should present that screen in a modal view, so that it is impossible to navigate to any other part of the app.
In other words, if you want to prevent a user from navigating away from a screen, don't show them buttons or tabs that would allow them to navigate away. Otherwise, you're just making more work for yourself and frustrating a user.
Implement UITabBarControllerDelegate in your app delegate's applicationDidFinishLaunching
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
self.tabBarController.delegate = self;
[window addSubview:self.tabBarController.view];
}
Then use the below delegate method,
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
This method is called before the tab switch and you can return no here to disable that and show an alert message instead. Once the user has performed the save either he can press on tab again or you can programmatically switch to the new tab as,
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Add this inside your delegate,
How about this for switching to the tab programmatically,
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if () {
//some code
} else {
//some other code
self.tabBarController.selectedViewController = viewController;
}
}

IOS: iPad keyboard always visible?

In my application, I want the keyboard to always be visible in a view. I can do it with a textfield in this way:
[textField1 becomeFirstResponder]; //in viewWillAppear
but I don't have a text field explicitly in the view itself?

Showing UISearchBar "X" within text field vs. adjacent Cancel button

I know I can set showsCancelButton to NO for a UISearchBar ... until you tap the search bar text field, and then the cancel button appears anyway! (At least it does for me.)
Is there a way to show the "X" in a circle within the UISearchBar text field vs. having that separate Cancel button show up adjacent to it? Note that this button only appears for me when search mode is active, and this is regardless of the setting for showsCancelButton.
try this
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
mySearchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = NO;
}
when using this cancel button will not shown.
for (UIView *searchBarSubview in [searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setClearsOnBeginEditing:YES];
}
}
to enable the cancelButton try searchBar.showsCancelButton = YES;
Try the above lines of code.
if you use interface builder it is ery easy to show.it is there by default.i have used search bar in my app and the searchbartextfield has an "X" to clear the textfield
It might be that the "X" is only intended to clear the search field and not to cancel the search, and so the Cancel button must remain once a search is in play, regardless of showsCancelButton.