UITextfield textfieldchange event not fired - ios

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

Related

How to create Custom Delegate of UITextField in Objective C

I am new in iOS and I am facing problem regarding to create Custom delegate of UITextField
My code is like this
In CustomTableView
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([self.delegate respondsToSelector:#selector(textFieldShouldEndEditing:)])
{
[self.delegate textFieldShouldEndEditing:textField];
}
return YES;
}
In View Controller
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
But this method is not getting call in ViewController.
Can Any body please tell me what I am doing wrong
Thanks in Advance!
You should not call textField's delegates methods manually like [self.delegate textFieldShouldEndEditing:textField];
If you want to call your delegate method from another class then make some method which is not provided by sdk!
for example make method like,
- (BOOL)myTextFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
and call this method from your other class by your self.delegate and make sure that self.delegate is not nil, i mean it have object of your viewController!
you need to create a custom TextField class and also set delegate in that class. Like this
.h File
#interface CustomTextField : UITextField<UITextFieldDelegate>
#end
.m File
#implementation CustomTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.delegate = self;
}
return self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
#end

UITextField method call when done button click

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

resignFirstResponder and - (BOOL)disablesAutomaticKeyboardDismissal {return NO;} isnt hiding the keyboard, why isnt it working?

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

How to dismiss all keyboards of multiple text fields?

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

More then one UITextField keyboard dismiss on UIButton Click in iOS

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

Resources