Lovely Lovely - 1 month ago 6
Objective-C Question

Check code for saving textfield.text in a dictionary

-(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);

}


Kindly check the above code.I am trying to save the text on every textfield of custom cell in dictionary on click of save button.But it actually stores the text entered on last textfield.Please tell why is so happening.I am a fresher and not able to understand this.

Answer

remove this userDetailsDictionary= [[NSMutableDictionary alloc] init]; in delegate method of textFieldDidEndEditing and add in viewDidLoad,

for e.g

- (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"];
    }


}
Comments