UITextField in UITableViewCell and validation in m

2019-06-09 19:56发布

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!

2条回答
乱世女痞
2楼-- · 2019-06-09 20:33

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

查看更多
闹够了就滚
3楼-- · 2019-06-09 20:36

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.

查看更多
登录 后发表回答