I have a view controller which presents another view controller and within the presented viewcontroller , user makes choices which will lead to many other notifications to be pushed but when we dismiss the presentation viewcontroller I would like original parent viewcontroller to be notified as viewwillappear is not firing.
iOS get notified in parent viewcontroller when presented viewcontroller is dimissed
1.1k Views Asked by D C T At
2
There are 2 best solutions below
3

You can add a block handler to your child controller to notify the parent controller with the user choice:
struct Choice {
// whatever object that represents the user choice
}
class ChildController: UIViewController {
var completionHandler: ((ChildController, Choice) -> Void)?
func finishPresentation(with choice: Choice) {
// Suppose this function is called when user picks something in the user interface
completionHandler?(self, choice)
}
}
Then in parent controller, assign the completionHandler
to get notified with the user choice.
class ParentController: UIViewController {
func presentChild() {
let controller = ChildController()
controller.completionHandler = { child, choice
child.dismiss(animated: true) {
// do something with the user choice
}
}
present(controller, animated: true)
}
}
You may post the notification from child when it's about to be dismissed and observe the notification in parent view controller.
In parent view controller
In child viewcontroller when you choose to dismiss , send the notification
Adopt the class
UIAdaptivePresentationControllerDelegate
in presented view controlleradd below delegate methods
}