Matt Spoon
Matt Spoon

Reputation: 2840

Swift: Reload a View Controller

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?

Upvotes: 49

Views: 176672

Answers (9)

Sandeep Vishwakarma
Sandeep Vishwakarma

Reputation: 467

In Swift 5:

self.view.layoutIfNeeded()

Upvotes: 15

Tommy Flaherty
Tommy Flaherty

Reputation: 1

func clearBackground(){
        loadView()
        view.backgroundColor = .systemBackground
    }

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

Upvotes: 0

Andy
Andy

Reputation: 107

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.

Upvotes: -7

ballercoder1
ballercoder1

Reputation: 179

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

Upvotes: 0

dimaskpz
dimaskpz

Reputation: 87

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

Upvotes: 0

Ben Spector
Ben Spector

Reputation: 329

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

Upvotes: 10

iAnurag
iAnurag

Reputation: 9346

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

Upvotes: 26

Ahmed Khedr
Ahmed Khedr

Reputation: 1073

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

Upvotes: 5

Edward Anthony
Edward Anthony

Reputation: 3464

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()

Upvotes: 13

Related Questions