How to refresh a view?

Hi,


I’ve been trying to refresh a view to reload its data,

for example, let’s say the user has a name called Bill,

if im going to another view to change his name to Jack,

when Im going back to the previous view to see his name changed,

it’s still Bill, only if I’m running the app again it’s showing the changed result- Jack.


Is there a way to refresh the view every time I’m going back to that view?

So I have come to that conculsion that is not a view issue, it's a collection view issue,

it doesn't reload the buttons when the view appear again, even if I do a func button to reloadData()

it doesn't work.

Where do you reload() ?

Are you sure you have saved the new data in the datasource ?


The delegation mechanism should work without need to reload.

Delegate could also modify the datasource ; then on unwind from segue, call reload()

only if I'm running the app again, all the view controllers reload,

and all the like\unlike buttons in view controllers\collection view are correct by the real time database,

(if the current user liked\unliked the post)


I've tried delegation protocol, NSNotification, doing a singleton the DataManager.

it works to change text, but not buttons.


The buttons work like this: if the current user liked the post-

likeButton.isHidden = true

unlikeButton.isHidden = false


I'm really lost.


I want it the same way instagram\facebook do.

If you like\unlike\comment the post, it's gonna be updated in other views

and real time changes, if someone else liked it or commented, you can see it right away.

Do you understand that when the app is running in viewController "VC1" it takes the code you have written and creates a viewController called "VC2" using that code (aka "instantiates"). Then it transfers control of the program to that new viewController VC2. From that VC2 you have two choices: 1) You can create a new viewController that duplicates the code for VC1 but is a whole new VC1 with all new buttons and all new names that might be exactly the same as the first VC1 (aka "instantiates") or 2) you can go back to the pre-existing VC1 (aka "pop" or "dismiss").


And - any variables you assign within VC2 (e.g. "likeLabel") will not affect the variables in VC1 (e.g. "likeLabel") or in the newly instantiated VC1 (e.g. "likeLabel") even if they have the same name (e.g. "likeLabel").


Once you understand what is written above then, and only then, can you begin to solve your problems.


Also, see my post below.

Hey,


so the code for changing the name works, and also the singelton Model works.


I have tried to do dismiss or pop or instantiates but the buttons doesnt change

if I'm doing like\unlike in one view controller, the other one stays the same it was before.

only if I rerun the app.

From VC1 you will want to instantiate VC2 and run it. Then from within VC2 you will want to dismiss VC2 and thereby return to the parent pre-existing VC1. Upon returning to VC1 you will be running viewWillAppear. Within viewWillAppear in VC1 you will want to set your buttons and labels with the then new names from your Model (i.e. NSUserDefaults).

Ok I have done it all, besides the last part- NSUserDefaults.


I have a collectionView in VC1, so the button is inside every cell.

if the like button is hidden, unlike button isn't hidden, and opposite.


Is there a guide I can read to know how to use this NSUserDefaults with buttons?

There are two parts to this question:


>Is there a guide I can read to know how to use this NSUserDefaults with buttons?


The guide for NSUserDefaults is:

https://developer.apple.com/documentation/foundation/nsuserdefaults

It is a file system that your app can use to store variables or, in this case, to use as a simple Model.


How to use it with buttons would be here:

https://developer.apple.com/documentation/uikit/uibutton?language=objc

You are lost, but do not seem to follow what you are told to do. Or at least do not report what you are doing.


Have you done the following :


PostCommentController is VC1

NewsViewController is VC2


So

@IBAction func unwindToViewController1(_ sender: UIStoryboardSegue) {

print("I just unwind")

}

must be in VC1 PostCommentController, not in VC2 NewsViewController


In VC2, you have

func commentButtonTapped(at index: IndexPath) {

let vc = storyboard?.instantiateViewController(withIdentifier: "PostCommentController") as? PostCommentController

self.present(vc!, animated: true, completion: nil)

So, you instanciate a new VC1 !


Have you replaced present() by a self.dismiss

self.dismiss(animated: true, completion: nil)

Hey,


so the userDefaults really helped with the buttons!

The problem now is how do I set the buttons that are in the collection view cell(for every post individually),

in the ViewWillAppear?


I did collectionView.reloadData(),

but it's updating all of the buttons for every cell in the collection view.

(I updated the buttons for one cell in another view)

hey,


Yes I did all of that.

I can't do dismiss for VC2 NewsViewController, because there's nothing to dismiss, so it doesn't work.

also the unwind didn't work.

thanks for the help, your way is correct and it works for the text change,

the problem really was with the buttons,

and PBK helped me with that with NSUserDefaults

>I can't do dismiss for VC2 NewsViewController, because there's nothing to dismiss, so it doesn't work.


This sounds very very wrong. You must dismiss the child viewController (VC2) to get back to the parent (VC1). Otherwise you will be instantiating a new VC1 and that would be wrong. Basically, there is a hierarchy of viewControllers parent-child-grandchild. The parent is being viewed and has control and it creates the child and presents the child (i.e. displays the child and transfers control to the child). The child creates the grandchild (i.e. displays the grandchild and transfers control to the grandchild). When done, the grandchild dismisses itself (i.e. the child comes back into view and the child regains control). When done the child dismisses itself (i.e. the parent comes into view and gains control). If, instead of dismissing the child, you continue to create more and more 'parent' viewControllers then you will have multiple viewControllers all in memory at one time and each with a separate set of local variables.

>I did collectionView.reloadData(), but it's updating all of the buttons for every cell in the collection view. (I updated the buttons for one cell in another view)


This is correct, reloadData. That will redisplay the collectionView. And then in cellForRow (or the equivalent in your setup) you reconstruct each cell being displayed whether or not they have changed. Be sure to update your local variables in viewWillAppear based on values in NSUserDefaults (i.e. your Model) before calling reloadData - or else use the values from the Model (NSUserDefaults) in cellForRow.


I am not sure what you mean by "(I updated the buttons for one cell in another view)"

You said :

(I updated the buttons for one cell in another view)


Take care, you should not update the cell itself (it is not a storage item) but the data source drom which to feed the cells with cellViewAt

Just to make clear-

NewsViewController is actually the parent, because it's instantiating to the PostCommentController,

I called it VC2 becuase Claude31 called it that way, but it's actually VC1.

How to refresh a view?
 
 
Q