1

The app I'm working on has a main view controller, which is presenting different view controllers modally. These view controllers may present their own child view controllers modally.

At random times the main view controller may receive a notification to present a message (via message view controller) to the user, but the message view controller should not be presented until all other currently presented view controllers are dismissed.

Is there a reliable way for the main view controller to figure out when the modally presented view controller and its children are dismissed?

I've tried overriding the dismiss(animated:completion:) in the main view controller, but it is not called if the modally presented view controller calls self.dismiss(animated:completion:). I've also tried with overriding viewDidAppear(_:) in the main view controller, but it is not called if the modally presented view controller is presented with presentation style .overFullScreen.

Any ideas?

Edit: I'm not asking how to figure out if the main view controller is topmost, I'm asking how can the view controller know when it becomes the topmost view controller.

Srđan Stanić
  • 740
  • 1
  • 8
  • 17
  • see [here](http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller) – mfaani Jan 23 '17 at 19:34
  • 1
    Possible duplicate of [Get top most UIViewController](http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller) – Michael Fourre Jan 23 '17 at 19:37
  • Do you use Unwind Segue? – ppm Jan 23 '17 at 19:54
  • @ppm I'm not using unwind segue. Those modally presented view controllers are other people code and I may not be able to affect how they present their child view controllers nor how they dismiss themselves. – Srđan Stanić Jan 23 '17 at 19:59
  • @MichaelFourre Thanks for the suggestion, but it's not a duplicate. – Srđan Stanić Jan 23 '17 at 19:59
  • @Honey Unfortunately, that doesn't answer my question. I've added a clarification to the question. – Srđan Stanić Jan 23 '17 at 20:00
  • @SrđanStanić I didn't mean as a duplicate. I meant it can be helpful – mfaani Jan 23 '17 at 20:26
  • @Honey Ok, I appreciate the effort! – Srđan Stanić Jan 23 '17 at 20:33
  • @SrđanStanić That may be hard ;-) Is there any way to know those view controllers presented from main view controller (immediate child of mainVC) are dismissed? like delegate, etc. You may be assume it as all VCs are dismissed. – ppm Jan 23 '17 at 21:28
  • Add a BOOL property, and make it YES when the modals are presented. and on viewDidAppear on your "main" viewController, just check against the bool and you will know if the view appeared from a "popped subviews" or not. –  Jan 23 '17 at 21:30
  • @ppm that is precisely my question :) I'm trying to figure out when the immediate children are dismissed, because their children either already are dismissed or will be dismissed at that moment. What is puzzling for me here is this part of the Appl docs: _"To dismiss a presented view controller, call the dismissViewControllerAnimated:completion: method of the presenting view controller. You can also call this method on the presented view controller itself. When you call the method on the presented view controller, UIKit automatically forwards the request to the presenting view controller."_ – Srđan Stanić Jan 23 '17 at 21:34
  • @ppm But when dismiss() is called on the presented view controller, dismiss() on the presenting view controller does not get executed as far as I can see. – Srđan Stanić Jan 23 '17 at 21:36
  • @Sneak I'm not sure I'm following. The question already addresses the viewDidAppear() approach. Unless I'm misunderstanding your suggestion. – Srđan Stanić Jan 23 '17 at 21:40
  • @SrđanStanić I am not familiar with overFullScreen . The easiest solution and to avoid wierd notifications cent across the app, is just create a delegate on the first presented view controller. And when you present it , set its delegate to the mainViewController, and call the delegate when you pop the presented view. If you have multiple views, you can check in the navigationController if all the attached viewControllers are popped you can call the delegate. Maybe this is the solution for you? –  Jan 23 '17 at 22:03
  • @Sneak Those modally presented view controllers are other people's code and I am not able to modify them. Or at least I'm looking for a solution that does not require modifying their code. Also there is no navigation controller in this setup. – Srđan Stanić Jan 23 '17 at 23:11
  • @SrđanStanić http://stackoverflow.com/questions/29271880/viewdidappear-not-called-when-the-modal-view-is-dismissed –  Jan 23 '17 at 23:22

0 Answers0