I am in this situation:
I am passing 4 array from Progress Table to Detail Exercise using prepare for segue and it works fine! The problem begin when I try to pass the data back from the Detail Exercise Controller to the Progress Table Controller. I would like to use the default navigation back button to go back to the parent view. Actually I'm using this code but it doesn't work, well the data pass from the child to the parent view but i can't see the result in the Progress Table, seems like i need to refresh but i also tryed the reloadData after the viewDidLoad and it doesn't work. Any suggestion? Thank you.
override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController()){
print("n'drio")
let historyView = self.storyboard!.instantiateViewControllerWithIdentifier("historyView") as! HistoryTableViewController
historyView.isFirstTime = false
historyView.arrayData = arrayDataDetails
historyView.arrayRipetizioni = arrayRipetizioniDetails
historyView.arrayPeso = arrayPesoDetails
historyView.arrayRecupero = arrayRecuperoDetails
historyView.tableView.reloadData()
}
}
Also, you can use this extension:
When you press the back button, the navigation controller will call
navigationController(willShowViewController:)
so you can use this to pass the data back to your initial view controller. An example is shown below:Using
UINavigationControllerDelegate
:Swift 2:
Swift 3:
In this example, the key is using the
UINavigationControllerDelegate
and setting the delegate of the navigation controller (in this case it'sself
). Having done this, you can send the data back to your initial view controller with the back button.Personally I prefer using a class for my data:
Using a custom class for your data:
Progress view controller:
Details view controller:
In the last example, you don't have to worry about passing around data. Whenever you change the data, the controllers using the same class, will have the changes as well.
Typically, protocols and delegates are used to pass data back and forth between screens.
That being said, it seems non-standard to try to save the data when clicking the back button. Are you sure you don't want to do this on "Done" instead?
Simpler solution you can use self.navigationController?.viewControllers to access previous view controller as following:
updateData is the data member to update at previous view controller