45

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 then come back to it, it seems to work perfectly?

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

Forge
  • 6,538
  • 6
  • 44
  • 64
Matt Spoon
  • 2,760
  • 5
  • 25
  • 41

9 Answers9

26

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

iAnurag
  • 9,286
  • 3
  • 31
  • 48
  • 14
    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. – TM Lynch Mar 19 '20 at 15:49
  • 1
    Also, this reload data every time the view is shown, even if it not necessary. – Channel Jun 09 '20 at 16:34
  • depends on your navigation hierarchy. If you dismiss a non fullscreen modal, willWillAppear will not be called. – Shawn Frank Jul 15 '21 at 04:09
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()

Edward Anthony
  • 3,354
  • 3
  • 25
  • 40
  • 6
    How do you do the actual refresh interface part of this? – Tejas Sharma Jun 14 '16 at 05:43
  • 3
    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. – Vincent Jan 27 '17 at 09:29
  • // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly. – Piotr Badura Aug 11 '17 at 11:22
13

In Swift 5:

self.view.layoutIfNeeded()
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

Ben Spector
  • 319
  • 2
  • 9
5

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

Forge
  • 6,538
  • 6
  • 44
  • 64
Ahmed Khedr
  • 1,053
  • 1
  • 11
  • 24
  • 14
    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.** [moreHere](http://stackoverflow.com/a/8876346/1264893) – iluvatar_GR Jul 15 '16 at 14:32
  • I don't think he is saying to call it directly. He is saying to add code there. – Andrew Kinnie Apr 03 '17 at 04:59
  • In my case the viewController is disappearing (it's added to tabBar) – Nike Kov Apr 24 '23 at 17:56
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

dimaskpz
  • 81
  • 8
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

ballercoder1
  • 159
  • 6
0
func clearBackground(){
        loadView()
        view.backgroundColor = .systemBackground
    }

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

-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.

Andy
  • 107
  • 1
  • 8