1

I have implemented a UITableView in VC1 to display firstName and LastName of the user,and a edit Button at the top of the UITableView. When the user presses the button it navigates through navigation controller to VC2.

In VC2 the user can update the firstName and LastName Fields. I am saving the details Using NSUserDefaults as

NSUserDefaults*first=[NSUserDefaults standardUserDefaults];
[first setValue:textField1.text forKey:@"firstname"];

While I go back to VC1 and try to display the updated data its not working why is it so...?

NSUserDefaults*first=[NSUserDefaults standardUserDefaults];
cell.detailTextLabel.text=[first valueForKey:@"firstname"];

How can I do it using NSUserDefaults.?

Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51
remyr3my
  • 768
  • 1
  • 6
  • 19

4 Answers4

1

NSUserDefault is working fine, you just need to reload your table on viewWillApear event.

//In VC1

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_yourTableView reloadData];
}
Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
0

I guess you should use setObject:forKey

NSUserDefaults*first=[NSUserDefaults standardUserDefaults];
[first setObject:textField1.text forKey:@"firstname"];
John Snow
  • 19
  • 2
0

Try to put breakpoint, after entering new "firstName" && "lastName". And in console print

po [first objectForKey:@"firstName"]

if its your value, then you should dig deeper in updating/refreshing data in first VC. Like [MyTable reloadData] or some.

Mr.Fingers
  • 1,105
  • 1
  • 11
  • 15
-1

Try below line of code

NSUserDefaults *first=[NSUserDefaults standardUserDefaults];
[first setValue:textField1.text forKey:@"firstname"];
[first synchronize]; // have to write this line of code.


NSUserDefaults *first=[NSUserDefaults standardUserDefaults];

cell.detailTextLabel.text=[first valueForKey:@"firstname"];
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51