Updating NSUserDefaults data in one ViewController changed in another ViewController

I have two view controllers, MainController and OptionsController. The OptionsController can be accessed using the MainController button. There are some values ​​in OptionsController that I can store using NSUserDefaults. These values ​​are required in MainController. If I change these values ​​in OptionsController when I go back to MainController they don't change, but if I run MainController again these values ​​were changed correctly. It looks like when the MainController is activated again, after exiting the OptionsController, the viewDidLoad is no longer created. How can I update the data in the MainController please?

+3


source to share


2 answers


viewDidLoad

Called only once in the life of a view controller , as Rounack says . If you have code that you want to execute every time a view controller is displayed, put it in viewWillAppear

.



NSUserDefaults

is a rather heavy method of transferring information between objects in memory (it writes to disk). If you don't need persistence between runs, one of the other options he suggested would be better. I would suggest a delegate pattern or completion block.

+4


source


viewDidLoad is called only once for a view controller. You can use NSNotification, KVOs, blocks, or something like a delegation pattern to get a callback in your MainController whenever a value changes inside OptionsController.



Otherwise, you can write code in the viewWillAppear that is called every time you exit to the options controller.

+3


source







All Articles