I have a app I'm making that has 2 text fields and buttons below it. When the user starts to type, the keyboard comes up and covers the buttons. Is there a way to make the keyboard go away?
There are two ways to do so
// this one line will dismiss the keyboard from anywhere in your code
[self.view endEditing:YES];
or
with the delegate of textfield (you should declare it in your .h file first)
#interface someController : UIViewController <UITextFieldDelegate>
and the in .m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// next line will dismiss the keyboard
[textField resignFirstResponder];
return YES;
}
Check UITextField documentation:
To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
In other words, just send resignFirstResponder to an active text field object.
I use this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self.alertView dismissWithClickedButtonIndex:self.alertView.firstOtherButtonIndex animated:YES];
return YES;
}
In the view controller after setting the text field delegate to self.
Related
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 have a button and a textfield. I just want keyboard disappear when clicking on button. Why my code below doesn't work.
Update: I saw something about file owner. I don't understand how to do this in XCode4 I use storyboard and I can't see any file owner icon.
Update 2: I found a tut http://www.techotopia.com/index.php/Writing_iOS_4_Code_to_Hide_the_iPhone_Keyboard_%28Xcode_4%29 but it uses XIB file on XCode 4 not storyboard. How to do this with storyboard ?
myViewController.h
#interface myViewController : UIViewController <UITextFieldDelegate>
{
UITextField *myTextField;
}
#property (retain, nonatomic) IBOutlet UITextField *myTextField;
myViewController.m
- (BOOL)textFieldShouldReturn: (UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myTextField.delegate = self;
}
- (IBAction)DoCalc:(id)sender {
// ...
}
Check to see if the textFieldShouldReturn function that you've written into myViewController.m is even being called. Set a breakpoint inside of it and then run the simulator. Press the return key on the keyboard. If the program doesn't break, then the function you've written isn't being called.
If it is, it's because you haven't delegated UIResponder duties to your view controller. Make sure you are delegating responder duties to your myViewController class for the UITextField that you're working with.
In storyboard, you do this by control-dragging from the UITextField widget to the yellow orb below the scene, and selecting "delegate" from the context menu that appears.
textFieldShouldReturn: should return NO to hide the keyboard. One more thing - do not set first responder to self, [textField resignFirstResponder] is enough, iOS should figure nextResponder by itself.
You can hide the keybord in this method...
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
Remove [self becomeFirstResponder]; should do it.
resignFirstResponder is to dismiss the keyboard. while becomeFirstResponder is to open the keyboard. so in your code, you are closing then opening again the keyboard at the same time. The last action is opened the keyboard, hence it won't close.
you may look into UIResponder Class as superclass of UITextField
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 editable text box and when I click on it I get a keyboard. But, it won't let me close out of the keyboard. When I go into the properties of the editable text object ( the Text View object ) and make the return button "Done" it still won't let me exit out.
You should do it programatically, by using resignFirstResponder message on the UITextField is currently being edited with the keyboard. Check this Stack Overflow question on the issue.
First, you need to make sure you controller is a UITextField Delegate in your .h- something like this:
#interface UserAddEditController : UIViewController <UITextFieldDelegate>
2nd, you need to implement the following:
- (BOOL)textFieldShouldReturn:(UITextField *) theTextField {
[theTextField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
Finally, make sure your UITextField's delegate is set to your File Owner in interface builder.
That should do it!
I am trying to recreate something similar to the popup keyboard used in safari.
I am able to visually reproduce it by placeing a toolbar over my view and the appropriate buttons, however i cant figure out any way to dismiss the keyboard once the user has touched the done button.
There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.
If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.
If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:
gameField.delegate = self;
gameField.returnKeyType = UIReturnKeyDone;
You must also implement the UITextFieldDelegate for your ViewController.
#interface YourViewController : UIViewController <UITextFieldDelegate>
Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]
-(BOOL) textFieldShouldReturn:(UITextField*) textField {
[textField resignFirstResponder];
return YES;
}
All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the
resignFirstResponder your set.
Have you tried:
[viewReceivingKeys resignFirstResponder];
where viewReceivingKeys is the UIView that is receiving the text input?
If your building your own views in Interface Builder, set your view controller to be delegate for the text field and implement textFieldShouldReturn: from UITextFieldDelegate in your views controller.
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(#"%# textFieldShouldReturn", [self class]);
[theTextField resignFirstResponder];
// do stuff with the text
NSLog(#"text = %#", [theTextField text]);
return YES;
}
UITextFieldDelegate textFieldShouldReturn: in the iphone cocoa docs
If you're talking about dismissing the keyboard from a UITextField rather than a UITextView. Your question isn't that clear? If you are then ensure your class is marked as a UITextFieldDelegate in the interface file,
#interface MyController: UIViewController <UITextFieldDelegate> {
UITextField *activeTextField;
// ...remainder of code not show ...
}
and then you should implement the two delegate methods as below,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeTextField = textField;!
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
activeTextField = nil;
[textField resignFirstResponder];
return YES;
}
However if you're using a UITextView then things are a bit more complicated. The UITextViewDelegate protocol lacks the equivalent to the textFieldShouldReturn: method, presumably since we shouldn’t expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return).
However, there are several ways around the inability of the UITextView to resign as first responder using the keyboard. The usual method is to place a Done button in the navigation bar when the UITextView presents the pop-up keyboard. When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard.
However, depending on how you’ve planned out your interface, you might want the UITextView to resign when the user taps outside the UITextView itself. To do this, you’d subclass UIView to accept touches, and then instruct the text view to resign when the user taps outside the view itself.
Create a new class,
#import <UIKit/UIKit.h>
#interface CustomView : UIView {
IBOutlet UITextView *textView;
}
#end
Then, in the implementation, implement the touchesEnded:withEvent: method and ask the UITextView to resign as first responder.
#import "CustomView.h"
#implementation CustomView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void) awakeFromNib {
self.multipleTouchEnabled = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touches began count %d, %#", [touches count], touches);
[textView resignFirstResponder];
[self.nextResponder touchesEnded:touches withEvent:event];
}
#end
Once you’ve added the class, you need to save all your changes, then go into Interface Builder and click on your view. Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your CustomView rather than the default UIView class. Then in the Connections Inspector, drag the textView outlet to the UITextView. After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. Note however that if the UIView you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. So while this solution is elegant, it can be used in only some situations. In many cases, you’ll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard.
use a navigation controller and pop the view when done?
for example, I use code like this to slide an about box in:
[[self navigationController] presentModalViewController:modalViewController animated:YES];
and then when the button in that about box is clicked, I use this to get rid of it:
[self.navigationController dismissModalViewControllerAnimated:YES];
In my case the about box occupies the whole screen, but I don't think it would have to for this to work.
edit: I think I may have misunderstood your question. Something along the lines of my code would be if you are faking the whole keyboard view yourself. I think that resign first responder is the right way to do it if it is the normal keyboard with your toolbar added on top.