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];
Related
I want the keyboard to disappear when the user presses the "return" button, but I have tried resignFirstResponder, endEditing:YES and -
(BOOL)disablesAutomaticKeyboardDismissal {return NO;}and the keyboard still didn't disappear. What should I do?
Here's the relevant code: (everything else is empty (viewDidLoad is empty, etc.)
#implementation ViewController {
__weak IBOutlet UITextField *textfield;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textfield resignFirstResponder];
//[self.view endEditing:YES]; (I uncommented this out and tested it)
return YES;
}
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
plz confirm your UITextField Delegate From Storyboard or set
textfield.delegate = self;
in - (void)viewDidLoad
and use the methode
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
I have outlet the connection of UITextField and written this code
-(void)viewDidLoad
{
[txtName addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange :(UITextField *)theTextField{
NSLog(#"Text changed");
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField==txtName)
{
[self.view endEditing:YES];
[self performSelector:#selector(ShowNames) withObject:nil afterDelay:0.1];
return NO;
}
return YES;
}
But -(void)textFieldDidChange :(UITextField *)theTextField{ is not fired
Couldn't understand where Am I going wrong
Any help would be appreciated
Replace the code method
-(void)textFieldDidChange :(UITextField *)theTextField{
NSLog(#"Text changed");
}
with
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
If you want to UITextField events you should use UITextFieldDelegate
In your .h file adopt UITextFieldDelegate
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
and then in your .m file
- (void)viewDidLoad
{
[super viewDidLoad];
[_txtName setDelegate:self];
}
Let me know if you have any issues
You've got the right idea, but your problem is within your -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField method.
Right now, before editing this method is called, and if the method returns NO, then the -(void)textFieldDidChange :(UITextField *)theTextField method will not be called.
Your if statement tests to see if the textField that should start editing is the same as txtName. If it does you are returning NO instead of YES. I think you just need to swap your NO's and YES's and you'll be golden.
Try this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField==txtName)
{
[self.view endEditing:YES];
[self performSelector:#selector(ShowNames) withObject:nil afterDelay:0.1];
return YES;
}
return NO;
}
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
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];
}