好的,我们开始:
感谢@Lefteris 和他存储当前索引的想法。由于我无法将索引存储到tag
属性中,我决定存储 activeindexPath
和另外的 active textField
。(我知道,参考UITextField
就足够了,但我需要它来做其他事情)
首先,我添加了这两个属性:
@property (nonatomic, strong) NSIndexPath *activeIndexPath;
@property (nonatomic, strong) UITextField *activeTextField;
然后我实现了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;
}
在textFieldDidEndEditing:
我通过使用方法将值存储到我的属性中(例如self.firstName
、self.lastName
等...)[self assumeInput:input withIndexPath:self.activeIndexPath];
。
在我的saveAction
-Method 中,我存储了当前活动的值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);
...
}
……就是这样!
希望能帮助到你!感谢@Lefteris 的意见。
最好的,克里斯