0

I know this is a frequently asked question and probably the most debated one on Stackoverflow overall. However I have now been searching for the answer for about one week without any luck.

Here is what I want to do..

I have two ViewControllers lets call them mainVC and settingsVC.

In my settingsVC I have a function where the user can put their hourly salary in a textfield that then saves using UserDefaults. This is the data I want to access in the mainVC.

However they don't have any connection to each other (their not bind by any segue and I don't want them to be) and I don't want the mainVC to be instantiated when I save my data.

Furthermore I do not want to use global variables. So what is the best way to do this?

To conclude I simply want to retrieve the data from my settingsVC in my mainVC without using global variables and segues.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • You can access data with `delegate` and `NotificationCenter ` – Harshal Valanda Nov 20 '17 at 12:36
  • 1
    No, the *most debated one on Stackoverflow overall* related to Swift is *unexpected found `nil`...* ;-) – vadian Nov 20 '17 at 12:38
  • UserDefaults are set for the whole application. Not per ViewController. So if you save them in your defaults you can acces them in another ViewController by UserDefaults.standard.double(forKey: <#T##String#>) if your salary is a double. – Mar-k Nov 20 '17 at 12:41
  • What about accessing user defaults in main VC? – Sweeper Nov 20 '17 at 13:20
  • Thanks for your answer Harshal Valanda. I think Thats the way to do it! Do you think you could go into a Little bit more detail on how to execute this! – albin öberg Nov 20 '17 at 13:41

3 Answers3

0

You should Use Instance variables , Protocols or Notification for passing data between viewContoller.

Dattatray Deokar
  • 1,923
  • 2
  • 21
  • 31
-2

Why wouldn‘t you retreive the data from the user defaults? That‘s where the data is stored and is universally available.

George Brown
  • 1,134
  • 10
  • 25
  • 2
    Userdefaults are not meant and design for persistency or passing data around. – vikingosegundo Nov 20 '17 at 12:47
  • Unless they're defaults, like setting a unit, or a default wage. The other vc is described as a settings vc, and user defaults would be where to store such a value, and retrieve it from. Userdefaults even caches values, so the file isn't opened every time. You can even configure user defaults to back up to icloud, and persist data across all of a users devices. It's not meant for storing large amounts of data, it's not a replacement for something like core data, there are set limits on how much data should be stored for iCloud, if userdefaults aren't meant to persist data, what are they for? – George Brown Nov 20 '17 at 15:15
  • 1
    Lets say VC A is writing something to UserDefauts. VC B accesses it, displays it. Now user leaves VC B. What happens now? exactly. nothing. And the data you passed from A to B stays in memory. That is why it can't be used to pass data around — which is the scope of the question. Hitting any kind of persistency just for passing stuff around is crazy, mad and overkill. – vikingosegundo Nov 20 '17 at 15:42
  • if you look at the question with no context sure, but you ask what happens to the data when the vc goes away, it persists so the user doesn‘t have to set their hourly wage everytime they open the app. They can overide a system default, because i can‘t see the specified hourly wage being zero being any kind of use in any app, so a default must be specified and stored somewhere anyway. – George Brown Nov 22 '17 at 10:19
  • 1
    the question is about passing data. user defaults is an inappropriate tool for that. your last comment is a totally different scenario. – vikingosegundo Nov 22 '17 at 11:26
-2

You are saving your data in NSUserDefaults. So you have no chance to worry for saving and retrieving data.

To save value

NSString *valueIWantToSet = @"valueToSave"
[[NSUserDefaults standardUserDefaults] valueIWantToSet forKey:@"saveKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

To get this value

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"saveKey"];

In case of not saving values in NSUserDefaults, you have to use Notification

Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25