ggworean ggworean - 7 months ago 9
Swift Question

Data is persistent in one table view, but not when switching between other view controllers

I'm looking to implement a contacts list into my app. I have the add contact working and it actively uses NSCoding for writing to the disk for the contacts. It will update properly between the list of contacts and the new contact view, but doesn't when I switch from another view.

Do I need to

prepareforsegue
when coming from the non-contact views in a way that prepares the data? Or do I need to create my
Set<Contacts>
in the first view and pass references through that when going between views. As of now it only updates data when I do my
unwindsegue
from the add contact view controller.

Answer

I would need more context (code) to be sure this will solve your case.

But I had similar issues in the past for 1 simple reason:

I put the refresh code in ViewDidLoad(). This is called only when the ViewController is loaded (first time it appears), not every time you switch back to it (doesn't reload it).

You should put your code in ViewDidAppear(), or ViewWillAppear() if you want it executed each time you "switch" back to your view.

Here is your code:

ViewDidAppear(){

some code
some code

code that refreshes view with contacts

}

Here is what it should be:

ViewDidLoad() {

some code 
some code
}

ViewDidAppear() {
code that refreshes view with contacts
}

Code that you want executed each time your view appears on the screen should be put in "ViewDidAppear()".

Comments