Disabling Keyboard - iphone

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.

Related

UITextfield keyboard resign

in my uitableview in each cell I'm having uitextfield when user edits the textfield and after pressing any other button on the screen the Keyboard is not resigning. I did the following in textfield delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textfieldInCell = textField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
textfieldInCell=nil;// this is the ivar i am using for each textfield in cell.
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
I am also calling the shouldReturn delegate function once the user tapping on any other button but the keyboard is not resigning. Where am I going wrong?
Confirm that you bind the delegate of each textfield that you create with the view controller and add one line of code in textfield did end editing :-
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// add the following line
[textField resignFirstResponder];
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
have you bind delegate of textfield with your controller? or did you check textFieldShouldReturn calling or not?
i think, binding is missing wit view controller in your case.
thanks
add a line in your tableview where you are adding textfield.
<YOUR_TEXTFIELD>.delegate = YES;
Enjoy Programming
first check that you give the delegate or not and give the delegate to UITextField after that when you call the method of button at that time resign this textfield like bellow...
-(IBAction)yourButton_Clicked(id)sender{
[yourTextField resignFirstResponder];
////your code write here
}
you must try to assign tag value to textfield then in should return method you used that tag to hide the keyboard like this (if you assign the tag -1 then)
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag==-1){
[textField resignFirstResponder];
}
}

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

i have used keypad in my program its not working

I have two textfields which are of type UITextField.
I have two buttons click and cancel.
The idea is to enter two numbers and it will navigate to next view. This View should display those two numbers.
The problem is if I click on the textfield one then the keypad is responding; when I click return it does not jump to the next field.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{[textField resignFirstResponder];return YES;}
You need to connect UITextFiled Delegate from nib or programeticuly like this image:
or
other way is programmatic like in your viewDidLoad method or viewWillAppear method:
Yourtxtfild.delegate = self;
EDIT
for jumping AnotherView Use this textfiled delegate method:-
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
//you can set your textfiled string in to globle Variable and use it in another viewController
YourViewCntroller *objViewController = [[YourViewCntroller alloc] initWithNibName:#"YourViewCntrollernib" bundle:nil];
[self.navigationController pushViewController:objViewController animated:YES];
}
You have to set the delegate of the textfield as the object of the class where you have written the textFieldShouldReturn: method.
#Ragavan : You are just resigning the first textfield, but you are not making the other textfield to respond. So, use the code below as reference to make it work.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
return YES;
}

how can I dismiss keyboard on Enter keypress

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

iPhone: Possible to dismiss keyboard with UITextField clear button?

I'm wondering if there is a way to have the UITextField clear button 'always visible'
textfield.clearButtonMode = UITextFieldViewModeAlways;
doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?
Thanks in advance.
Like mentioned before it seems apple is setting the textfield focus after you clear the field.
The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
In your delegate, the function
- (BOOL)textFieldShouldClear:(UITextField *)textField
is called when the users wants to clear the textfield. If you return YES and call
[textField resignFirstResponder];
the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.
edit To make sure you really resign the responder, try doing it just a little later:
[textField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.1];
The delay didn't work well for me. Instead I added an instance variable to the delegate:
BOOL cancelEdit;
Then in the delegate implementation:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (cancelEdit) {
cancelEdit = NO;
return NO;
} else {
return YES;
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
cancelEdit = YES;
return YES;
}
UITextFieldDelegate textFieldShouldClear
- (BOOL)textFieldShouldClear:(UITextField *)textField {
[textField] resignFirstResponder];
return YES;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.
If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.