1
votes

So one of the users in here managed to show me how to pass data from a child view controller to a parent view controller via a string.

So now the string is passed, BUT, i want that value to stay displayed on the firstViewController after the app is closed and re-opened.

The value is saved in with NSUserDefaults by the way and with an NSLog i am seeing on the conosole it is saved in the apps folder but that value isnt saved onto the UILabel display.

It only displays it when i put save but then i close and reopen, it dissappears but in an NsLog it is still inside the app but not on display UILabel.

How can i address this ?

On my appDelegate.h i have a

 @property (strong, nonatomic) NSString *sharedString;

To pass the secondViewController data to the firstViewController.

In the save method on my secondViewController i have a function related to the AppDelegate.h declaration which is:

AppDelegate *apiDelegate = [[UIApplication sharedApplication] delegate]
apiDelegate.sharedString = self.textFieldData.text;

And in my firstViewController i have a method which display the data from the second viewController:

   -(void) viewDidAppear:(BOOL)animated {
    AppDelegate *apiDelegate = [[UIApplication sharedApplication] delegate]
    self.DisplayData.text = appDelegate.sharedString;
     [super viewDidAppear: NO];

Is there something wrong which isnt keeping the data intact after app closes or am I missing something here ?

2
Why not just populate it from NSUserDefaults? And are you synthesizing the sharedString in the appdelegate? - bizsytes
You might also want to change [super viewDidAppear: NO]; to [super viewDidAppear: animated]; - bizsytes
I would recommend you not use the app delegate to pass data between classes. There are better methods such as public properties, delegates, etc. that are better coding practice. - Andrew

2 Answers

4
votes

So one of the users in here managed to show me how to pass data from a child view controller to a parent view controller via a string.

First you need to establish some hierarchy as to how you get a childViewController from a parentViewController. One way to pass data from childViewController to parentViewController is using a delegate. The other could be using the KVC/KVO protocol. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

In this you can simply register an observer for the property defined in the childViewController and observe it's changes wherever you want (well, given the hierarchy is satisfied).

To save the value. You can simply save it using NSUserDefaults. I don't see any code in your post but you can simply define a key and save the value with NSUserDefaults using:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sharedString forKey:@"sharedString"];
NSString *sharedStringFromDefaults = [defaults objectForKey:@"sharedString"];

Also,

AppDelegate *apiDelegate = [[UIApplication sharedApplication] delegate]

Apple requires you to avoid such references in the application. It only constrains the app. Further, the sharedString is not required to be in the AppDelegate. Otherwise the AppDelegate will be filled with almost every other data structure you have shared in the app.

1
votes
//add this code when you want to store string
[[NSUserDefaults standardUserDefaults] setObject:self.textFieldData.text forKey:@"sharedString"];
[[NSUserDefaults standardUserDefaults] synchronize];

//and when you want string than
self.DisplayData.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"sharedString"];