-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSInteger field = textField.tag;
userDetailsDictionary= [[NSMutableDictionary alloc] init];
if(field ==1)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"FirstName"];
}
if(field ==2)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"MiddleName"];
}
if(field ==3)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"LastName"];
}
if(field ==4)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"Country"];
}
if(field ==5)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"State"];
}
if(field ==6)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"City"];
}
if(field ==7)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"DOB"];
}
}
- (IBAction)saveAction:(UIBarButtonItem *)sender {
NSLog(@"the values stored in dictionary are %@",userDetailsDictionary);
}
请检查上面的代码。我试图在单击保存按钮的字典中保存自定义单元格的每个文本字段上的文本。但它实际上存储了在最后一个文本字段中输入的文本。请告诉我为什么会这样。我是更新鲜,无法理解这一点。
答案 0 :(得分:1)
正如其他人已经指出的那样,您应该在viewDidLoad
方法中初始化字典,然后只需将值设置到该对象中。也不要忘记将你的dict声明为strong
财产。
我将改进您的代码,使其更具可读性:
@interface ViewController ()
@property (nonatomic, strong) NSMutableDictionary *userDetailsDictionary;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_userDetailsDictionary= [[NSMutableDictionary alloc] init];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"cell%ld.userDetailTextField.text %@", (long)textField.tag, textField.text);
switch (textField.tag) {
case 1: _userDetailsDictionary[@"FirstName"] = textField.text; break;
case 2: _userDetailsDictionary[@"MiddleName"] = textField.text; break;
case 3: _userDetailsDictionary[@"LastName"] = textField.text; break;
case 4: _userDetailsDictionary[@"Country"] = textField.text; break;
case 5: _userDetailsDictionary[@"State"] = textField.text; break;
case 6: _userDetailsDictionary[@"City"] = textField.text; break;
case 7: _userDetailsDictionary[@"DOB"] = textField.text; break;
}
}
@end
答案 1 :(得分:0)
在userDetailsDictionary= [[NSMutableDictionary alloc] init];
的委托方法中移除此textFieldDidEndEditing
并添加viewDidLoad
,
例如
- (void)viewDidLoad {
[super viewDidLoad];
userDetailsDictionary= [[NSMutableDictionary alloc] init];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.tag ==1)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"FirstName"];
}
if(textField.tag ==2)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"MiddleName"];
}
if(textField.tag ==3)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"LastName"];
}
if(textField.tag ==4)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"Country"];
}
if(textField.tag ==5)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"State"];
}
if(textField.tag ==6)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"City"];
}
if(textField.tag ==7)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[userDetailsDictionary setObject:textField.text forKey:@"DOB"];
}
}
答案 2 :(得分:0)
在viewdidload
userDetailsDictionary= [[NSMutableDictionary alloc] init];
由于每次调用完成编辑时都会分配用户说明。
答案 3 :(得分:0)
这是因为每次调用textFieldDidEndEditing时都会初始化字典。 将init移至viewDidLoad方法。
-(void)viewDidLoad{
[super viewDidLoad];
userDetailsDictionary= [[NSMutableDictionary alloc] init];
}