Reputation: 532
1st VC --> 2nd VC --> 3rd VC then, pops back from 1st VC <-- 3rd VC.
The text of the cell selected on 3rd VC is stored in NSUserDefaults, and I use that text to fill a UIButton's text on the 1st VC.
The problem is that the text in the UIButton doesn't refresh/change it pops back to 1st VC from 3rd VC. The text is only refreshed when I stop the app and then re-run (in XCode Simulator).
So its saving the text in the NSUserDefaults correctly, but when it pops back to 1st VC, somehow the UIButton isn't refreshing.
Any help for how to get that UIButton to refresh when I pop back to the 1st VC?
-EDIT- Code snippets for 1st VC:
// Grab text from defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *teamNameOne = [defaults objectForKey:@"teamName1"];
// Set button Title
[buttonOne setTitle:teamNameOne forState:UIControlStateNormal];
Upvotes: 0
Views: 690
Reputation: 14427
Make sure that after you save the value to NSUserDefaults
in your 3rd VC, that you synchronize:
[defaults synchronize];
That updates the store with the recently set values.
Three other things to check:
Make sure that the button you are setting the title for is in fact in the Normal control state once the First VC gets loaded up.
Make sure the call to populate that button title in the First VC is done in viewWillAppear and not in viewDidLoad. When working with a navigation controller, the viewDidLoad isn't called when popping back to an already loaded view. Also make sure to call the super:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *buttonTitle = [defaults valueForKey:@"ButtonTitle"];
[self.myButton setTitle:buttonTitle forState:UIControlStateNormal];
}
Make sure you are populating the NSUserDefaults store prior to popping back to the First VC.
Here is a project I just built that does what you are needing (very simple project): https://dl.dropboxusercontent.com/u/3660978/SO_ViewControllerTransition.zip
Upvotes: 1
Reputation: 104082
This is not a good use for NSUserDefaults -- it's not meant to be a temporary holding place for values you're passing around in your app.
You should do this with an unwind segue. You can pass data back to VC1 in the prepareForSegue method, just like you would for a normal forward segue. You will also have a method in VC1 (the one you hook up the unwind segue to) that can be used to update your UI in that controller.
Upvotes: 1