In StoreViewController, when a button is clicked, a ModalViewController, named AddStoreVC is opened.
Then at AddStoreVC, when I press Save, I want to dismiss itself (which I can) and when StoreViewController is loaded, the tableView to be refreshed.
viewWillAppear, viewDidAppear or none of the alternatives on previous threads are applicable.
I'd appreciate for your support.
You can use a closure, delegate or notifications (and even KVO would also be a solution). Since you have a relation of one-one, I'd go with closure or pattern.
Add in the ViewController which will be modally presented (AddStoreVC)
var onWillDismiss: (() -> Void)?
When you call dismiss(animated:completion:)
on it, call onWillDismiss?()
In the presenting ViewController, get a reference on the modal one, and do:
modalVC.onWillDismiss = { [weak self] in
self?.myTableView.reloadData()
}
I passed no param (()
), but if you also want to retrieve a param, add it. Imagine you want an Int:
var onWillDismiss: ((Int) -> Void)?
onWillDismiss?(theIntIWantToPass)
modalVC.onWillDismiss = { [weak self] theIntPassed in
print(theIntPassed)
self?.myTableView.reloadData()
}
You can also use the delegate pattern:
Create the delegate:
protocol AddStoreVCCustomProtocol {
func modalVCWillDismiss(_ modalVC: AddStoreVC)
func modalVC(_ modalVC, willDimissWithParam param: Int)
}
Make the presenting one compliant to it:
extension StoreViewController: AddStoreVCCustomProtocol {
func modalVCWillDismiss(_ modalVC: AddStoreVC) {
myTableView.reloadData()
}
func modalVC(_ modalVC, willDimissWithParam param: Int) {
print("theIntPassed with delegate: \(param)")
myTableView.reloadData()
}
}
Add a property to the Modal to have a delegate:
weak var customDelegate: AddStoreVCCustomProtocol?
And call it on dismiss(animated:completion:)
: customDelegate?.modalVCWillDismiss(self)
or `customDelegate?.modalVC(self, willDimissWithParam: theIntIWantToPass)