It might be better to use an "unwind segue":
https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8
https://developer.apple.com/library/archive/technotes/tn2298/_index.html
It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack. And without the presented view controller having special knowledge of where it needs to navigate back to (i.e. you don't have to reach out directly to the window's root view controller).
Let's say you have some root view controller called VC1, the first modal is VC2, and the second modal is VC3.
In VC1 you would implement an IBAction
called (for instance) unwindToRoot
. Then in the storyboard for VC3, you wire up your Done button to the Exit
object and choose the unwindToRoot
action.
When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.
So basically you are just letting all these VC stack up, and when you are all done you are dismissing everything to return to the root VC.