3

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ketan Odedra
  • 1,215
  • 10
  • 35

6 Answers6

2

the shouldReturn delegate method is what you are looking for:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // do something
    return YES;
}
André Slotta
  • 13,774
  • 2
  • 22
  • 34
1

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.

Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20
0

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];
Dharman
  • 30,962
  • 25
  • 85
  • 135
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
0

You can check UItextfield's shouldreturn method call

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog( @"text changed: %@",textField.text);
    [self ProdcutDirectSetToCartAPICall];
    return YES;
}
Sunil Prajapati
  • 473
  • 5
  • 17
0

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;
}
Cintu
  • 913
  • 2
  • 16
  • 32
0

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;
}
Rajesh Dharani
  • 285
  • 4
  • 20