1
votes

I wanted to ask who should be the one dismissing a presented view controller?

Lets say I presented a view controller and on an IBAction in that view controller, I want to dismiss it. Should I be passing that responsibility to the presenting view controller by creating a delegate method or I should be just calling the dismissViewController:animated: on itself, which inturn anyways asks its presenting view controller to dismiss the presented view controller?

So, I think these are some clear cut cases where the presenting view controller should be the one dismissing the presented view controller

  1. The presented view controller is passing some data back to the presenting view controller.
  2. The presenting view controller wants to do something after the dismissal of the presented view controller.
  3. The presenting view controller to handle how the dismissal is going to happen, does it need some kind of animation

What if the presented view controller first checks if the presenting view controller actually wants to take the responsibility of dismissal by checking the presenting view controller implemented the dismissal delegate method? Is it really worth putting the complexity of conditional logic here?

And yes, I tried reading it on other forums and questions like

Dismissing a Presented View Controller

Dismissing Modal View Controllers

Present and dismiss modal view controller

view controllers: presentation, dismissal

But couldn't really find the right logical answer.

1
It's going to vary from scenario to scenario. In most cases it would be fine to call it on the presentedViewController to keep complexity low, but if there is underlying logic that needs to occur before or after dismissing then delegating back to the presenter would be better. There is no right or wrong answer, it is going to vary for the situation you are in.Tim
I get that, but its just that I still believe that calling the dismissal is from the presenting view controller is the best thing to do, as it doesn't have any downside, but just a few stupid lines of code. I just wanted to know if there is any stronger benefit in calling it from presenting or presented view controllerPuneet
The benefits can only be determined on a scenario by scenario basis. it depends on what you want to do when you dismiss. Otherwise, just use the simplest solution available and work your way up - dismiss from the presentedViewController internally unless you have any other reason not to.Tim

1 Answers

0
votes

Read this below link. You will get the idea how animation and presentation take place between viewcontrollers.

https://www.raywenderlich.com/113845/ios-animation-tutorial-custom-view-controller-presentation-transitions