Ketan Odedra
Ketan Odedra

Reputation: 1283

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

Upvotes: 3

Views: 3382

Answers (7)

Fattie
Fattie

Reputation: 12648

These days it's really this simple, fortunately

addAction({
        UIAction { [weak self] _ in
            print("that worked!")
            self?.field.resignFirstResponder()
        }
    }(), for: .editingDidEndOnExit)

Tip

It's editingDidEndOnExit, not editingDidEnd

Upvotes: 0

Pradumna Patil
Pradumna Patil

Reputation: 2220

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

Upvotes: 0

Rajesh Dharani
Rajesh Dharani

Reputation: 285

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

Upvotes: 0

Dishant Rajput
Dishant Rajput

Reputation: 1357

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.

Upvotes: 1

Cintu
Cintu

Reputation: 913

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

Upvotes: 0

Sunil Prajapati
Sunil Prajapati

Reputation: 473

You can check UItextfield's shouldreturn method call

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog( @"text changed: %@",textField.text);
    [self ProdcutDirectSetToCartAPICall];
    return YES;
}

Upvotes: 0

Andr&#233; Slotta
Andr&#233; Slotta

Reputation: 14030

the shouldReturn delegate method is what you are looking for:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // do something
    return YES;
}

Upvotes: 2

Related Questions