How to I go from one textfield to the next with the next button?
Here is my code that will not work:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.amountField) {
[theTextField resignFirstResponder];
} else if (theTextField == self.businessField) {
[self.memoField becomeFirstResponder];
}
return YES;
}
Call [textField resignFirstResponder]; before your first conditional
there are many solutions but I prefer this one:
[self.firstTextField addTarget:self.secondTextField action:#selector(becomeFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.secondTextField addTarget:self.thirdTextField action:#selector(becomeFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit];
just set it up in viewDidLoad
Related
I have two textfields as emailTextField and passwordTextField.
I have written the following textfield delegate methods for moving one text field to another:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == emailTextField) {
[textField resignFirstResponder];
[passwordTextField becomeFirstResponder];
} else if (textField == passwordTextField) {
[textField resignFirstResponder];
}
return YES;
}
When I click to next button after entering email then it will go to password textfield but within a fraction of a second, the keyboard will disappear.
What is the solution for this? Is there a mistake in the code?
Try this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == emailTextField) {
[passwordTextField becomeFirstResponder];
} else if (textField == passwordTextField) {
[textField resignFirstResponder];
}
return YES;
}
I have a textfield in a UITableViewCell
When I clicked in textfield directly this method is called:
-(void)editingChanged:(UITextField *)sender
this is my code in cellForRowAtIndexPath
[cell.ProductQuantityTextField addTarget:self action:#selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
-(void) editingChanged:(UITextField *)sender
{
NSLog( #"text changed: %#",cell.ProductQuantityTextField.text);
[self ProdcutDirectSetToCartAPICall];
}
I want that, when I click the textfield then I change the value of textfield and then I click done button of keyboard that time I want call UITextField method
the shouldReturn delegate method is what you are looking for:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do something
return YES;
}
There is a two way to do that
1.) Either you can perform UIControlEventEditingDidEndOnExit event.
2.) Or you can also implement your method in text field retun
<UITextfieldDelegate> //in yourController.h file
//yourController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//do your code here.
return YES;
}
Both method will help you to resolve your problem.
You can check UItextfield's shouldreturn method call
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog( #"text changed: %#",textField.text);
[self ProdcutDirectSetToCartAPICall];
return YES;
}
implement the <UITextFieldDelegate> protocol in your class, set
ProductQuantityTextField.delegate = self;
and use
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(#"Textfield text %#", textField.text);
[self ProdcutDirectSetToCartAPICall];
}
or
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I checked below code its working fine
In cellForRowAtIndexPath I added below code
cell.ProductQuantityTextField.delegate = self;
cell.ProductQuantityTextField.tag = indexPath.row; //If you want you can give tag here
[cell.ProductQuantityTextField addTarget:self action:#selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
-(void)editingChanged:(UITextField *)field
{
NSLog( #"text changed: %#",field.text);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog( #"textFieldShouldReturn: %#",textField.text);
[textField resignFirstResponder];
//If you need cell
TableViewCellName *cell = (TableViewCellName*) [[textField superview]superview];
NSLog( #"text changed: %#",cell.ProductQuantityTextField.text);
[self ProdcutDirectSetToCartAPICall];
return YES;
}
I think forControlEvents:UIControlEventEditingChanged is called every time when you change some contents inside the UITextField. If you want to call that method when user clicks 'Done' button on your keyboard then try changing the line
[cell.ProductQuantityTextField addTarget:self action:#selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
with
[cell.ProductQuantityTextField addTarget:self action:#selector(editingChanged:)UIControlEventEditingDidEnd];
I have three text fields in one view controller and when i do the method to dismiss the keyboard for all three text fields, the view controller doesn't come out.
- (void)viewDidLoad
{
[super viewDidLoad];
self.namesuite.delegate = self;
self.createpassword.delegate = self;
self.createname.delegate = self;
// Do any additional setup after loading the view.
}
I also have the textFieldShouldReturn function.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
This is my button function to send me to the view controller that has the text fields.
- (IBAction)createaccount:(id)sender
{
[self performSegueWithIdentifier:#"thirdsegue" sender:sender];
}
This will help. Try this:
[self.view endEditing:YES];
Hope this helps .. :)
You should try this control (TPKeyboardAvoiding) and get ride of resign and scrolling issues. Its a generic solution.
Try:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.namesuite resignFirstResponder];
[self.createpassword resignFirstResponder];
[self.createname resignFirstResponder];
return YES;
}
or
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.namesuite)
{
[self.namesuite resignFirstResponder];
}
else if (textField == self.createpassword)
{
[self.createpassword resignFirstResponder];
}
else if (textField == self.createname)
{
[self.createname resignFirstResponder];
}
return YES;
}
In my class,i have more then 30 UITextField and one UIButton. When I click the uibutton i need to resign the keyboard of all uitextfield without giving resignfirstresponder to all 30 uitextfields.
My code like this
-(IBAction)click:(id)sender
{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
[txt3 resignFirstResponder];
[txt4 resignFirstResponder];
.
.
.
[txt30 resignFirstResponder];
}
I need simple way to resign UITextField keyboard
Every view has endEditing property , try simply this
[[self view] endEditing:YES];
and let me know whether it works for you or not ..
UITextField *currentTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
-(IBAction)click:(id)sender
{
[currentTextField resignFirstResponder];
}
Then simply in the method below add
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
or
in the delegate method
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
-(void)KeyboardHide
{
[txt1 resignFirstResponder];
....
....
[txt30 resignFirstResponder];
}
-(IBAction)click:(id)sender
{
[Self KeyboardHide];
}
I have two fields, fldPassword and fldUsername. With this code (and a self delegate in the view did load), I would expect the return key to dismiss at both of the text fields. However, it only does it on the first. What am I doing wrong?
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (fldPassword == self->fldPassword) {
[fldPassword resignFirstResponder];
} else if (fldUsername == self->fldUsername) {
[self->fldUsername becomeFirstResponder];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
in your .h file add delegate method :
#interface YourViewController : UIViewController<UITextFieldDelegate>
then it will work for both textfields.
hope this helps u.
You are dismissing only the fist one, try this:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (fldPassword == self->fldPassword) {
[fldPassword resignFirstResponder];
}
if (fldUsername == self->fldUsername) {
[self->fldUsername resignFirstResponder];
}
return YES;
}