Skip to content

Instantly share code, notes, and snippets.

@bcye
Created April 3, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcye/8fa53524e6d529162a0750471fdda0a3 to your computer and use it in GitHub Desktop.
Save bcye/8fa53524e6d529162a0750471fdda0a3 to your computer and use it in GitHub Desktop.
How to call a function of a vc inside another vc.
//Inside the main vc i put a function like this
//Function fetches data and calcs the total
func changeNavTitle() {
//[...]
// It does some stuff and then updates navigation label (you can change this to another label)
titleNav.title = "\(sum)\(currencySymbol)"
fetchedResultsController.changedContent = false
}
//Inside the other vc I call the function like this:
//I have an IBAction that's calling the dismiss
//If your dismiss is handled otherwise and you can't put both in one function, you can still add the code in the
//completion of dismiss
//If you need help with the completion, watch this treehouse course: https://teamtreehouse.com/library/closures-in-swift
@IBAction func save(_ sender: Any) {
//[...] does some stuff for CoreData
//Calls the function to update the total
//IMPORTANT: Change the "TableViewController" to the class name of your vc. Else it wont be able to access that function!!!
let presentedBy = presentingViewController as? TableViewController
presentedBy?.changeNavTitle()
dismiss(animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment