Force Refresh on Another Viewcontroller Component with Swift

Force refresh on another ViewController component with swift

I think, you want to add data from oneViewController and without pressing button, you want after navigation or pressing back button, you want to reload automatically tableview to show updated results, right.

If I'm not wrong then, this solution will work for you.

Follow this below steps:

Step 1 :

Add this code in your view controller, from which you want to add data in array or in database, instead of button click.

 override func viewDidLoad()
{
super.viewDidLoad()
let backItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")
self.navigationItem.leftBarButtonItem = backItem
}

func goBack()
{
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
self.navigationController?.popViewControllerAnimated(true)
}

Step 2:

Now its final step.
Now add this below code, to your result view controller, where you want to automatically update the result.

 func loadList(notification: NSNotification){
self.TableView.reloadData()
}

override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)

}

Now implement this code-stuff in your coding. Hope it works for you.

How to reload tableview from another view controller in swift

You can do this:

In your tableView Controller:

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}

@objc func loadList(notification: NSNotification){
//load data here
self.tableView.reloadData()
}

Then in the other ViewController :

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

How to reload a view controller when back from another view using tab bar

Follow these steps to handle you view load setup every time your view will appear:

(VC1 = First View Controller)

  • Create a new function/method (named viewLoadSetup) in your VC1 and move all codes from viewDidLoad() to viewLoadSetup().
  • Now, Call viewLoadSetup() from viewWillAppear

    class VC1: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    // viewLoadSetup() you may call it from view did load also
    }

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    viewLoadSetup()

    }

    func viewLoadSetup(){
    // setup view did load here
    }

    }


If you want to call viewLoadSetup from viewDidLoad for one time, when your view controller is loaded, and then after every time from your viewWillAppear then,

class VC1: UIViewController {

var isLoadingViewController = false

override func viewDidLoad() {
super.viewDidLoad()
isLoadingViewController = true
viewLoadSetup()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if isLoadingViewController {
isLoadingViewController = false
} else {
viewLoadSetup()
}
}

func viewLoadSetup(){
// setup view did load here
}

}

How to reload data in a TableView from a different ViewController in Swift

Xcode 9 • Swift 4

Create a Notification Name:

extension Notification.Name {
static let reload = Notification.Name("reload")
}

You can post a notification

NotificationCenter.default.post(name: .reload, object: nil)

And add an observer at the view controller that you want to reload the data:

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reloadTableData), name: .reload, object: nil)
}

@objc func reloadTableData(_ notification: Notification) {
tableView.reloadData()
}

SwiftUI: Forcing an Update

Setting currentPage, as it is a @State, will reload the whole body.

Reloading a ViewController

You really don't need to do:

[self.view setNeedsDisplay];

Honestly, I think it's "let's hope for the best" type of solution, in this case. There are several approaches to update your UIViews:

  1. KVO
  2. Notifications
  3. Delegation

Each one has is pros and cons. Depending of what you are updating and what kind of "connection" you have between your business layer (the server connectivity) and the UIViewController, I can recommend one that would suit your needs.

How can I force my parent ViewController to reload after unwinding from a child?

If you put the refreshing code in viewDidAppear in the ViewController then it should be called any time that the ViewController is displayed. Therefore, it will be shown initially, and after the back button is pressed.

This is different than placing it in viewDidLoad, which will only be called the first time the ViewController is loaded.



Related Topics



Leave a reply



Submit