how can I dismiss keyboard on Enter keypress - iphone

I want to dismiss my keyboard as I press RETURN key.
I have tried by putting button in view's back side.
But how can I do this by pressing RETURN key?

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Don't forget to add the delegate UITextFieldDelegate

I'm presuming you're talking about a UITextField rather than a UITextView as your question isn't that clear? If so 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.

I hope you have done UIViewController <UITextFieldDelegate> and yourTextField.delegate=self;
and then in the delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
[textField resignFirstResponder];
return YES;
}

Make sure your view controller class is a delegate for your UITextField and then use the delegate method in that class:
#pragma mark - Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// Dismiss the keyboard when the Return key is pressed.
[textField resignFirstResponder];
return YES;
}

Related

Open a new view when tapping a UITextField (Round Style Text Field)

I'm trying to open a Table View Controller when I tap a UITextField.
I added a push segue to my UITextField. However, rather than switching to
the new view, a keypad is opening.
What can be the problem?
1st in your .m file declare the UITextView delegate:
#interface ViewController ()<UITextFieldDelegate>
in viewDidLoad:
self.textField.delegate = self;
Implement the textFieldDidBeginEditing:(UITextField *)textField method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//segue over here
}
Try implementing the textFieldShouldBeginEditing: UITextViewDelegate method. It would look something like:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == myTextField) {
// Open the new ViewController here
return NO;
}
return YES;
}
You also have to set the delegate: myTextField.delegate = self;
you need to check out the UItapGestureRecognizer. you need to add TapGesture Recognizer on textField when user tapped on textField. then call tapGestureRecognizer method and do what you want here.
UITapGestureRecognizer * recog=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped:)];
recog.numberOfTapsRequired=1;
recog.delegate=self;
[self.view addGestureRecognizer:recog];
-(void)tapped:(UIGestureRecognizer *)gesture
{
// Do what you want here
}
2) 2nd way is
add TextField delegatei in .h file.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//Do what you want here..
}
try this one...

UITextField return key on subview

I've got a UIViewController with an additional small UIView I created on top of it (subview). When I click a button this view hovers to the center of the screen. The issue is the i've got a UITextField in the additional UIView and i cannot seem to get the return key to work.
Although I set my IBAction to the event "Editing did end" of the text field, when i click the return key, the IBAction doesn't run.
What am I doing wrong?
you just set Your Delegate for example :- yourtextfile.delegate=self; and also dont forget to add delegate method in to .h file
#interface contacts_detailView : UIViewController<UITextFieldDelegate>
and then you delegate textFieldDidEndEditing
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//your new view apear code here
[textField resignFirstResponder];
}
make sure UITextFieldDelegate at interface
Clicking on "Return" doesn't trigger an "Editing did end" event. Only when you'll call resignFirstResponder on the UITextField will you see the event triggered.
What you should do is set your UIViewController as the delegate of the UITextField and implement the method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
First of all, delegate the TextField to the file's owner like this:
yourtextField.delegate = self in your viewDidLoad.
And then add this to the view controller
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
It should work.
There is no need to write more code for key board return. Please write this in your .m file , it will work for any number of text field , no need to write again again for different textfield.
use <UItextfieldDelegate> in your .h file. Then make wiring with fileowner in nib file.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}

Can't resignFirstResponder with UITextView

I have a UITextView. I implemented a navigationBar UIBarButtonItem to respond to a touch and resign the firstResponder for my UITextView.
But, when the selector method is called, the keyboard doesn't get dismissed. I checked the UITextView's responder status with isFirstResponder and it returns YES. I also checked it with canResignFirstResponder and the return value is NO.
I must be missing something here...why is it returning NO?
I get that I can override canResignFirstResponder by subclassing UITextView, but I'd like to avoid that if possible.
Here's a code snippet:
- (void) commentCancelButtonTouched:(id)sender
{
NSLog(#"Cancel button touched");
[self.navigationBar popNavigationItemAnimated: NO];
if ([self.textInput.textView canResignFirstResponder] == NO) {
NSLog(#"I don't want to resign!");
}
[self.textInput.textView resignFirstResponder];
}
Just in case anyone wants to hide the keyboard when you touch outside of the textview, it's pretty easy...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.xFront resignFirstResponder];
}
xFront is my outlet to my UITextView.
Addressing your UITextField by way of a UIView subclass is a little non-standard. If I were you I'd get myself a direct handle on that UITextField right from inside your view controller. Set it up as a synthesized property of your view controller, give it the keyword IBOutlet so you can talk about it in Interface Builder, then make sure the outlet is hooked up to the text field.
In other words, rather than talking about self.textInput.textView, you want to be talking about self.textView directly. And double-check your IB outlet hookup, because I suspect that's where the real trouble is.
I figured it out. I had a 'UITextViewDelegate' method in my 'textInput' view, textViewShouldEndEditing that was overrided to return NO. Changed it to YES and it solved the problem.

iPhone keyboard, Done button and resignFirstResponder

This is probably a dumb question, but I can't find the answer in the docs. Did the "Done" button on the pop-up keyboard always cause the keyboard to disappear? I see a lot of code around the web like this:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
When I press the "Done" button, the keyboard pops down and the UITextField resigns first responder.
I'm presuming that pressing the "Done" button didn't used to cause a UITextField to resignFirstResponder, but that behavior changed at some time.
I'm debugging on OS 3.0 - 3.1.3
I made a small test project with just a UITextField and this code
#import <UIKit/UIKit.h>
#interface TextFieldTestViewController : UIViewController
<UITextFieldDelegate>
{
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
#import "TextFieldTestViewController.h"
#implementation TextFieldTestViewController
#synthesize textField;
- (void)viewDidLoad
{
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
[self.textField addTarget:self
action:#selector(textFieldFinished:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[super viewDidLoad];
}
- (IBAction)textFieldFinished:(id)sender
{
// [sender resignFirstResponder];
}
- (void)dealloc {
[super dealloc];
}
#end
The text field is an unmodified UITextField dragged onto the NIB, with the outlet connected.
After loading the app, clicking in the text field brings up the keyboard. Pressing the "Done" button makes the text field lose focus and animates out the keyboard. Note that the advice around the web is to always use [sender resignFirstResponder] but this works without it.
In Xcode 5.1
Enable Done Button
In Attributes Inspector for the UITextField in Storyboard find the field "Return Key" and select "Done"
Hide Keyboard when Done is pressed
In Storyboard make your ViewController the delegate for the UITextField
Add this method to your ViewController
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
From the documentation (any version):
It is your application’s
responsibility to dismiss the keyboard
at the time of your choosing. You
might dismiss the keyboard in response
to a specific user action, such as the
user tapping a particular button in
your user interface. You might also
configure your text field delegate to
dismiss the keyboard when the user
presses the “return” key on the
keyboard itself. 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.
So, you have to send resignFirstResponder somehow. But there is a possibility that textfield loses focus another way during processing of textFieldShouldReturn: message. This also will cause keyboard to disappear.
One line code for Done button:-
[yourTextField setReturnKeyType:UIReturnKeyDone];
And add action method on valueChanged of TextField and add this line-
[yourTextField resignFirstResponder];
I used this method to change choosing Text Field
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField isEqual:self.emailRegisterTextField]) {
[self.usernameRegisterTextField becomeFirstResponder];
} else if ([textField isEqual:self.usernameRegisterTextField]) {
[self.passwordRegisterTextField becomeFirstResponder];
} else {
[textField resignFirstResponder];
// To click button for registration when you clicking button "Done" on the keyboard
[self createMyAccount:self.registrationButton];
}
return YES;
}

Disabling Keyboard

How to Hide Keyboard by pressing Returnkey
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.
alt text http://www.thoughtblog.com/imgs/delegate.png
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.returnType = 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.
The keyboard only shows up when something editable (usually a UITextField) has become the first responder. Therefore, to make the keyboard go away, you have to make the textField not be the firstResponder anymore. Fortunately, it's one line of code:
[myTextField resignFirstResponder];
You really need to include more information with your question, but I think this might be what you are looking for:
First, make your view controller implement the UITextFieldDelegate:
#interface MainViewController : UIViewController <UITextFieldDelegate> {
Then add this method to the controller:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Read the UITextFieldDelegate documentation to see what else you can do.
Use these two methods:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeTextField = textField;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
activeTextField = nil;
[txtPassword resignFirstResponder];
[txtUserName resignFirstResponder];
return YES;
}
Please make sure you have given delegates to each textfields. For that you should go to the view. Right click . set the delegate to view.