I have a first tableViewController which opens up a second tableViewcontroller upon clicking a cell. The second view controller is presented modally (Show Detail segue) and is dismissed with:
self.dismissViewControllerAnimated(true, completion: {})
At this point, the second view controller slides away and reveals the first view controller underneath it. I would then like to reload the first view controller. I understand that this may require use of delegate functions, but not sure exactly how to implement it
You can simply reaload your data in
viewDidAppear:
, but that might cause the table to be refreshed unnecessarily in some cases.A more flexible solution is to use protocols as you have correctly guessed.
Let's say the class name of your first tableViewController is
Table1VC
and the second one isTable2VC
. You should define a protocol calledTable2Delegate
that will contain a single method such astable2WillDismissed
.Then you should make your
Table1VC
instance conform to this protocol and reload your table within your implementation of the delegate method.Of course in order for this to work, you should add a property to
Table2VC
that will hold the delegate:and set its value to your
Table1VC
instance.After you have set your delegate, just add a call to the delegate method right before calling the
dismissViewControllerAnimated
in yourTable2VC
instance.This will give you precise control over when the table will get reloaded.
I solved it a bit differently since I don't want that dependancy.
And this approach is intended when you present a controller modally, since the presenting controller wont reload when you dismiss the presented.
Anyway solution!
Instead you make a Singleton (mediator)
Have you Presenting controller implement the protocol like this:
and finally in your PresentedViewController in your viewDid/WillDisappear func or custom func add: