-1

I have 2 View Controller's, one of them has a string (e.g. "Text1 Text2") that when a button gets pressed it saves it to default user data:

[[NSUserDefaults standardUserDefaults] setObject:textObject forKey:@"textObjectKey"];

This same button has a popover presentation segue onto the 2nd view controller that in its viewDidLoad retrieves the string:

[[NSUserDefaults standardUserDefaults] stringForKey:@"textObjectKey"];

and calls a setText method to a textView to contain the string.

This does work, but my problem is that when I change the string (e.g. "Text1 Text2 Text3") and click the button the textView does not show the next text until I press it again. Not only that, but if I run the project again the first time I press the button it will have the text from the last time I ran the program (until I click it again).

Is this 'delay' something I can fix? Or is it a bug with iOS (I think this only because it keeps info from the previous program execution).

Fix edit: Rookie mistake. I put the statement that stores the string in user data inside of the button method with the segue.

sp00ky
  • 151
  • 9
  • 1
    You're saving data to a persistent store (NSUserDefaults) and are wondering why it shows that data on the next execution of the program? – RyanR Jun 05 '15 at 21:26
  • Ah ok then. Sorry I'm a bit of a noob – sp00ky Jun 05 '15 at 21:27
  • And the reason for your "delay" is simply that your code to set the data is called after the code to load it (try settings breakpoints). – Daniel Rinser Jun 05 '15 at 21:28
  • Unless you reset the simulator/device, anything you save in the appropriate application directories or to `NSUserDefaults` will persist across runs and rebuilds of the applications. Can you post the relevant ViewController code from project? – RyanR Jun 05 '15 at 21:29
  • This is the code from the view controller that loads the string from user data: NSString *textString; - (void)viewDidLoad { [super viewDidLoad]; textString = [[NSUserDefaults standardUserDefaults] stringForKey:@"textString"]; _textView.text = textString; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } – sp00ky Jun 05 '15 at 21:34
  • @sp00ky Don't post your code in comments. Update your question with further details so everyone sees it when reading the question. – rmaddy Jun 05 '15 at 21:45
  • You should set the text field value in `viewWillAppeae`, not `viewDidLoad`. The text field property will stil be nil at that point – Paulw11 Jun 05 '15 at 22:40

1 Answers1

0

If this is all you're doing with the text, I would say it is a slow solution. Setting a property on the next controller directly would be better in my opinion. But, after storing to NSUserDefaults, call

[[NSUserDefaults standardUserDefaults] synchronize];

Otherwise it is written to persistent storage when it feels like it.

Julian Minde
  • 166
  • 3
  • 7
  • 1
    How does calling `synchronize` help with the delay (hint: it doesn't)? – rmaddy Jun 05 '15 at 21:35
  • Yeah I was doing that and it didn't make a difference – sp00ky Jun 05 '15 at 21:41
  • Well I thought the next vc would have to wait for the automatic periodic synchronization, which could cause a delay. – Julian Minde Jun 05 '15 at 21:42
  • @JulianMinde The values are in `NSUserDefaults` immediately. You can read a value immediately with no delay. The persistence only affects the data being available the next time the app runs. – rmaddy Jun 05 '15 at 21:44