I have a UISearchBar. I enabled the 'cancel' button so it says cancel right next to the search box how can i set that button to simply lower the keyboard when the user presses it?
For this there's one delegate method called when cancel button is pressed
-(void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
[searchBar resignFirstResponder];
}
-Happy Coding....
Related
This is my code below. I can call my numpad but I can't hide it. How to hide it?
- (IBAction)loopBtn:(id)sender {
loopBtn.keyboardType = UIKeyboardTypeNumberPad;
[loopBtn becomeFirstResponder];
[sender resignFirstResponder];
}
You can hide keyboard by making your UIView an UIControl (UIControl is subclass of UIView). Then Write an IBAction method that is executed when you touch that UIControl(formerly UIView). Open interface editor and select main UIView. Then open identity inspector and change it class to UIControl from UIView. Now in your viewcontroller write an IBAction method.
-(IBAction)hideKeyboard:(id)sender
{
[self.view endEditing:YES];
}
Now connect your UIView's TouchUpInside event to this IBAction method. This will hide keyboard whenever you touch outside of your textfield.
Simple question: how can I resign my textfield if my "done" key is Search. Essentially, what do I do if the user does not want to search, but instead wants to cancel...
thanks
You can use: a cancel button for SearchBar and need to implement this SearchBar delegate :
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
isSearching = NO; //This is a flag which specifies if searching is going on or not.
searchBar.text = #""; //Clears out search bar text
[self resetSearch]; //Reset search resets all the flags and variables.
[self.leadsTable reloadData]; //Reloads the tableView
[searchBar resignFirstResponder]; //Resigns responder from Search bar
}
This is a proper way to resign the responder if user doesn't want to search.
Look at how you can add an in-built Cancel button in UISearchBar. Check the property "Shows Cancel Button" (Red Arrow highlight)
EDIT:
STEP-1:
Check conditionally whether your textField's text is blank? If so resignFirstResponder for the TextField. You need to set the Delegate to self for the UITextField using code:
txtField.delegate = self;
Or you can do the same from the interface builder by connecting TextField's delegate to File's Owner.
STEP-2: You need to implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField.text isEqualToString:#""])
{
[textField resignFirstResponder];
}
return YES;
}
EDIT-1:
Now create a UIToolbar with one bar button labeled 'Cancel'
Once you have done that:
You can write an button click event for UIToolBar:
-(IBAction)cancelClicked:(id)sender
{
[txtField resignFirstResponder];
}
Once you have done that you can now just write:
txtField.inputAccessoryView = toolbarOutlet;
Hope this helps you.
The text on the return button is irrelevant to the discussion. You want to know how to resign first responder without pressing the return button, and there are a few ways to do it.
Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard.
Use a tap gesture recognizer on the field's superview to recognize when the user taps outside the field, and call [self.view endEditing:YES] (where self is your view controller). This will cause the first responder to resign. (This is very finicky in a scroll view.)
Swap out the rightBarButtonItem of the current view controller for a cancel bar button item while editing, assuming you have a UINavigationBar on screen at the time. When editing ends, swap back in the regular right bar button item, if any.
I use a UITextView in my application. The text editing is ok. I set the return button to Done.
When I finish the editing, I like to hide the keyboard with the done button.
My Question: How can I set the done button?
Thanks,
Balu.
Set your controller as the textField's Delegate, implement UITextField Delegate method textFieldShouldReturn in your controller and resign first responder before returning TRUE/YES:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I have a standard UINavigationController setup. One particular view displays an "Edit" button on the right side of the Navigation Bar. When that button is pressed and the view switches to editing mode I would like to replace the "Back" button on the left side with an "Add" button. Of course, when editing is finished (the user presses "Done") the button on the left side should change again to the back button.
The obvious answer works. Assuming you have declared a UIBarButtonItem called addButton, you can implement setEditing:animated: as follows:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
self.navigationItem.leftBarButtonItem = addButton;
} else {
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
}
}
Is there a way to dismiss the keyboard from MFMailComposeViewController ? If the user rotates the device, I am loading a separate controller without "send" or "cancel" being pressed, and the keyboard is remaining on screen. Is there an way to dismiss the keyboard without "done" or "send" being pressed?
You can find the first responder and ask it to resign active which should dismiss the keyboard.
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];