I have a text label on view 1 and a button. If I click the button, I am brought to view 2 through a modal connection. In this view, I enter a number and press a button. The button saves the number to NSUserDefaults, as well as tries to update the text label on view 1 to reflect this number.
Button's code:
- (IBAction)returnToView1:(UIButton *)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
defaults setObject:@"myNumber" forKey:@"myKey"];
[defaults synchronize];
_myLabel.text = [defaults stringForKey:@"myKey"];
}
However, when I go back to view 1 using a modal connection, the label never updated. I could solve this by adding the following code:
-(void)viewDidAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_myLabel.text = [defaults stringForKey:@"myKey"];
}
The problem is, view 1 first loads, and then the text field populates, so it looks unprofessional. I want the text label to populate before loading the view. I've tried placing this code inside of this method:
(void)viewWillAppear:(BOOL)animated
...but that didn't work either (for some reason the text field would only populate after I closed the app, switched to view 2, pressed the button a second time, and then returned to view 1). Thanks for any suggestions!