ios - NSUserDefaults Saving only last entered data -
i saving textfield text using nsuserdefaults on button click in viewcontroller2 code
buttonclick() { nsuserdefaults *userdefaults =[nsuserdefaults standarduserdefaults]; [userdefaults setobject:keywordfield.text forkey:@"keywords"]; [userdefaults synchronize]; } viewdidload method retrieving -(void)viewdidload { nsuserdefaults *userdefaults =[nsuserdefaults standarduserdefaults]; [keywordarray addobject:[userdefaults objectforkey:@"keywords"]]; [userdefaults synchronize]; }
here assigning keywordarray tableview cell.textlabel.text =[keywordarray objectatindex:indexpath.row];
first entered text(first) in textfield , click button showing in table first second time entered text (second) in textfield , click button showing table first second here working fine. problem when come viewcontroller2 viewcontroller1. showing last entered value second
what going wrong here?
try this
- (ibaction)buttonclick:(id)sender { [keywordarray addobject:keywordfield.text]; nsuserdefaults *userdefaults =[nsuserdefaults standarduserdefaults]; [userdefaults setobject:keywordarray forkey:@"keywords"]; [userdefaults synchronize]; [selt.tableview reload]; //reload table every time new text added } -(void)viewdidload { if([[nsuserdefaults standarduserdefaults] objectforkey:@"keywords"]==nil){ keywordarray = [[nsmutablearray alloc] init]; } else{ keywordarray = [[nsuserdefaults standarduserdefaults] objectforkey:@"keywords"]; } }
Comments
Post a Comment