UITextfield keyboard resign - iphone

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

Related

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

UITextField always resignFirstResponder

I am using a UIPickerView and I want my keyboard to never show up for some of my UITextFields. When I call resignFirstResponder on the UITextField after it is touched it doesn't make the keyboard go down.
- (IBAction) txtFieldClicked:(id)sender {
[txtField resignFirstResponder];
}
For each text field where you don't want to be able to show the keyboard, do:
[textField setUserInteractionEnabled:NO];
You can also check a corresponding block in the Interface Builder UI. This prevents the text field from receiving the touch event that would otherwise trigger it to display the keyboard.
Set text field delegate and implement its delegate methods
See UItextFieldDelegate
If you dont want the keyboard to pop up, return NO for all those text fields here
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqualTo:self.myTextField]) {
return NO;
}
return YES;
}

Resign keyboard when leaving textfield

Hey, I have created a textfield within two custom cells. One textfield shows the standard keyboard and the other the shows a pickerview when entered. The problem I have is when I move from the keyboard to pickerview textfield without clicking the "return" button on the keyboard, the keyboard doesn't resign. However when I do it using the "return" the keyboard resigns. Am using:
- (void)textFieldDidEndEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
}
and can't work out why this isn't working.
Thanks,
William
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == pickertextfield)
{
[textfield1 resignFirstResponder];
[pickertextfield resignFirstResponder];
}
return YES;
}
Does your view controller adopt <UITextFieldDelegate>? Is it hooked up as the delegate of your text fields?
Try using [self.view endEditing:YES]; before showing the pickerview and see it it helps.

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.