0

I am using the approach described in this stackoverflow posting to retrieve values from a textfield. My problem is that the tableview is presented modally and I have a save button that validates the input and stores it.

The problem is that the textFieldDidEndEditing method is not called when the user clicks an UIBarButtonItem (= the save button, which closes the modal view).

In this event (when the user wants to save the input) I would like to validate it. But the values are stored in properties in the textFieldDidEndEditing. Due to the fact that this method is not called, I cannot validate the input values correctly.

Does anyone have a hint or solution on this?

Thanks in advance!

Community
  • 1
  • 1
Chris
  • 3,057
  • 5
  • 37
  • 63

2 Answers2

2

You should assign unique tag numbers to your text fields, then keep track on which is currently active (i.e. use a int iVar to store the active text fields tag value) in the textFieldDidBeginEditing delegate and when the user clicks the save, you should get the last textfield by it's tag value and then it's text value so you can validate it.

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • I can't use the `tag` attribute. I'm currently using it to retrieve the textfield from the cell (`[cell viewForTag:...]`). Any other suggestion? – Chris Jul 25 '12 at 07:32
  • Thanks for your input. Great idea with storing the index but I have improved it a little bit. – Chris Jul 25 '12 at 09:53
  • I don't see any reason why you can't access the textview by the tag, even if it's in a cell. Just use unique tag numbers, and once you get a reference to the cell, do a UITextView *myTextView = (UITextView*)[cell.contentView viewWithTag:XXX]; – Lefteris Jul 25 '12 at 14:16
  • Yep, this works (basically the same how I have done and described in my post below). I thought, how you have described in your post above, that I have to assign a unique tag number over all cells. The tag is unique **inside** a cell but not alongside all rows. – Chris Jul 25 '12 at 14:21
0

Okay, here we go:

Thanks to @Lefteris and his idea with storing the current index. Due to the fact that I cannot store the index into the tag attribute I decided to store the active indexPath and additionally the active textField. (I know, a reference to the UITextField would have been enough but I needed it for other stuff)

First I have added these two properties:

@property (nonatomic, strong) NSIndexPath *activeIndexPath;
@property (nonatomic, strong) UITextField *activeTextField;

Then I implemented textFieldDidBeginEditing: and textFieldDidEndEditing: of UITextFieldDelegate.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = (NSIndexPath*)[self.tableView indexPathForCell:(UITableViewCell*)[[textField superview] superview]];

    self.activeTextField = textField;
    self.activeIndexPath = indexPath;
}

- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    NSString *input = textField.text;    

    //assuming values from input textfield into corresponding properties
    [self assumeInput:input withIndexPath:self.activeIndexPath];

    self.activeTextField = nil;
    self.activeTextField = nil;
}

In textFieldDidEndEditing: I am storing the values into my properties (such as self.firstName, self.lastName, and so on...) by using the method [self assumeInput:input withIndexPath:self.activeIndexPath];.

In my saveAction-Method I am storing the value from the currently active TextField.

- (IBAction)saveButtonClicked:(UIBarButtonItem *)sender 
{    
    //assuming input from active field (didEndEditing _not_ called right now!)
    [self assumeInput:self.activeTextField.text withIndexPath:self.activeIndexPath];

    //test output
    NSLog(@"firstName: %@", self.firstName);
    NSLog(@"lastName: %@", self.lastName);
    NSLog(@"email: %@", self.email);
    ...
}

... and that's it!

Hope it helps! Thanks to @Lefteris for his input.

Best, Chris

Chris
  • 3,057
  • 5
  • 37
  • 63