Swift: Reload a View Controller
Asked Answered
S

9

47

I need to refresh a view controller when a certain button is tapped. Simply activating viewDidLoad() does not seem to be working for what I need to do. However, when I leave the view controller and then come back to it, it seems to work perfectly.

How can I refresh/reload a view controller as if I have left it and then come back to it without ever actually leaving it?

Stockman answered 3/7, 2015 at 13:16 Comment(3)
What exactly are you trying to refresh ?Harrumph
did you found any solution?Stinker
This is question is similar to another question I answered. Please look at my response here: updating a view controller with new dataConditional
O
26

Whatever code you are writing in viewDidLoad, Add that in viewWillappear(). This will solve your problem.

Occipital answered 3/7, 2015 at 13:20 Comment(3)
only if you leave the view controller and come back to it. But if you want to refresh the view controller when a button is tapped, this will not work.Postulant
Also, this reload data every time the view is shown, even if it not necessary.Ichinomiya
depends on your navigation hierarchy. If you dismiss a non fullscreen modal, willWillAppear will not be called.Beeeater
W
14

In Swift 5:

self.view.layoutIfNeeded()
Workaday answered 12/12, 2018 at 9:18 Comment(1)
It's a good answerElectrocute
D
13

You shouldn't call viewDidLoad method manually, Instead if you want to reload any data or any UI, you can use this:

override func viewDidLoad() {
    super.viewDidLoad();

    let myButton = UIButton()

    // When user touch myButton, we're going to call loadData method
    myButton.addTarget(self, action: #selector(self.loadData), forControlEvents: .TouchUpInside)

    // Load the data
    self.loadData();
}

func loadData() {
    // code to load data from network, and refresh the interface
    tableView.reloadData()
}

Whenever you want to reload the data and refresh the interface, you can call self.loadData()

Dumah answered 25/4, 2016 at 10:55 Comment(3)
How do you do the actual refresh interface part of this?Immiscible
When you want to reload, call loadData again to get the data from model. In a tableView call reloadData() or in a regular view setNeedsDisplay.Idealist
// This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.Increscent
H
9

If you are using a navigation controller you can segue again to the current UIViewController and it will be refreshed. Here I use a UIButton for refreshing

Hit answered 12/12, 2019 at 11:46 Comment(2)
This is the correct answer to force a refresh of the view controller on a button tap inside that view controller.Postulant
best answer so far!Foliage
R
5

This might be a little late, but did you try calling loadView()?

Richellericher answered 10/11, 2015 at 7:39 Comment(3)
public func loadView() // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly. moreHereQuarterhour
I don't think he is saying to call it directly. He is saying to add code there.Neutralize
In my case the viewController is disappearing (it's added to tabBar)Polygynous
O
0

View Life Cycle

it will load ViewWillAppear everytime you open the view. above is the link to the picture View Controller Lifecycle.

viewWillAppear()—Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen. Despite the name, just because the system calls this method, it does not guarantee that the content view will become visible. The view may be obscured by other views or hidden. This method simply indicates that the content view is about to be added to the app’s view hierarchy.

https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

Octavie answered 15/4, 2020 at 8:53 Comment(0)
L
0

If you need to update the canvas by redrawing views after some change, you should call setNeedsDisplay.

Thank you @Vincent from an earlier comment.enter image description here

Lightship answered 19/5, 2020 at 2:51 Comment(0)
A
0
func clearBackground(){
        loadView()
        view.backgroundColor = .systemBackground
    }

Calling this function for when a button is pressed worked perfectly for me to reload a view.

Annulment answered 12/8, 2022 at 18:55 Comment(0)
D
-7

Swift 5.2

The only method I found to work and refresh a view dynamically where the visibility of buttons had changed was:-

viewWillAppear(true)

This may be a bad practice but hopefully somebody will leave a comment.

Delphine answered 13/6, 2020 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.