7

I am on iOS 6 xcode 4.6.2 using storyboards.

I am using a dynamic UITableView which consists of a number of cells each of which have two UITextFields on them. The two fields are defined in a custom cell as

@property (strong, nonatomic) IBOutlet UITextField *lowRangeField;

@property (strong, nonatomic) IBOutlet UITextField *highRangeField;

I wish to use

-(void) textFieldDidEndEditing:(UITextField*) textfield

to get at the values and save it into a core data store.

Now, obviously, I can get at the value and assign it where I like, because I have the pointer to the textfield. My issue is I don't know how to identify which field on the cell this actually is. I know I can get the textfields superview to identify which cell its on , so I can work out which set of lowRangeField and highRangeField it is but then I get stuck.

peterh
  • 11,875
  • 18
  • 85
  • 108
SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24

3 Answers3

17

My issue is I don't know how to identify which field on the cell this actually is.

Use Tag to Identify.

lowRangeField.tag = 1;
highRangeField.tag = 2;


-(void) textFieldDidEndEditing:(UITextField*) textfield
{
if (textField.tag == 1) {
NSLog(@" clicked in lowRangeField"); 

} else if (textField.tag ==2) {
 NSLog(@" clicked in highRangeField");
}
}
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • So I guess that as these cells are created dynamically on a tableview I should set the tags when the cell is created in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. Would that be right ? – SimonTheDiver May 17 '13 at 11:59
  • If there is two fields on your Cell and want to identify which one Edit this will be fine. and when to identify Every Field that you created in App also can identify on the basis on which Cell. – Buntylm May 17 '13 at 12:03
  • Cheers Bunty - I have too little hair left to be puling it out in frustration – SimonTheDiver May 17 '13 at 12:04
  • 1
    @SimonTheDiver Ya.. set the tag for the textField in cellForRowAtIndexPath: – Anil Varghese May 17 '13 at 12:04
1

Try this one

This is used to identify in which text field you have entered value .

- (void)viewDidLoad
{
    lowRangeField.tag = 100;
    highRangeField.tag = 200;
}

-(void) textFieldDidEndEditing:(UITextField*) textfield
{
     if (textField.tag == 100)
     {
        NSLog(@" clicked On lowRangeField"); 

     } 
     else if (textField.tag ==200) 
     {
         NSLog(@" clicked On highRangeField");
     }
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

If you have two (or more) field referred in the ViewController (as property) you can distinguish them following this way:

- (void) textFieldDidEndEditing:(UITextField *)textField {
if (textField==self.lowRangeField)
//do something
if (textField==self.highRangeField)
// do something else
}