Skip to content

Instantly share code, notes, and snippets.

@jimbrayrcp
Last active May 9, 2020 13:51
Show Gist options
  • Save jimbrayrcp/5c0d8316cf4f79888a35833380da7185 to your computer and use it in GitHub Desktop.
Save jimbrayrcp/5c0d8316cf4f79888a35833380da7185 to your computer and use it in GitHub Desktop.
Swift 5: iOS: 13.1 Runs a script in another viewController: in this gists example, you can reload data in a TableView from a different ViewController
// --------------- VIEW CONTROLLER 1 -----------------------
// MARK: NOTIFICATION
extension Notification.Name {
static let reload = Notification.Name("reload")
}
// MARK: CALLER
NotificationCenter.default.post(name: .reload, object: nil)
// --------------- VIEW CONTROLLER 2 -----------------------
override func viewDidLoad() {
super.viewDidLoad()
// MARK: OBSERVER
NotificationCenter.default.addObserver(self, selector: #selector(reloadTableData), name: .reload, object: nil)
}
// MARK: METHOD
@objc func reloadTableData(_ notification: Notification) {
tableView.reloadData()
tableView.layoutIfNeeded()
}
@jimbrayrcp
Copy link
Author

To use:

  1. Create the NOTIFICATION extension
  2. Post the CALLER of the notification at the point in the current script at which you need to call the method from the other view
  3. In the view controller holding the method you desire to run, insert an OBSERVER for the notification in .viewDidLoad()
  4. Expose the METHOD with (@objc) and ensure method name is the same as the #selector from the OBSERVER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment