0

I have a UIViewController, MainViewController, that presents a SheetViewController using modalPresentationStyle = .formSheet. SheetViewController then presents another ViewController using modalPresentationStyle = .fullScreen. Doing this causes viewWillDisappear() to be called on SheetViewController, but never on MainViewController.

Since I have some frequent calls to a backend in MainViewController, I want to be informed when it is not visible, such that I can stop these calls. This could be done using delegates, but can it really be that there is no way that MainViewController can be informed directly by iOS when it is no longer visible on the screen?

JRV
  • 1,702
  • 1
  • 17
  • 27
  • Here is some sample code demoing the problem: https://gist.github.com/johan-mjolner/26b8d5fad2aa9cd3e520411576787750 – JRV Jan 04 '23 at 18:49
  • II'd use a simple closure to do so https://stackoverflow.com/a/45619821/1801544 – Larme Jan 04 '23 at 19:07
  • I know, but I would rather like iOS to inform me if possible - there are a LOT of classes involved in my legacy app, so I would rather not use a closure or delegates... – JRV Jan 04 '23 at 20:38

1 Answers1

0

The issue is related to how you are manipulating the view hierarchy.

If you present a VC using modalPresentationStyle = .fullScreen, UIKit removes the presenting controller.

If you present a VC using modalPresentationStyle = .formSheet, UIKit DOES NOT remove the presenting controller.

So...

From MainVC, present SheetVC as .formSheet ... MainVC is still in the hierarchy.

From SheetVC, present FullScreenVC as .fullScreen ... SheetVC is the presenting controller, so it is removed from the hierarchy -- but MainVC is still where it was.

When you dismiss FullScreenVC, SheetVC is again added to the view hierarchy, on top of MainVC.

DonMag
  • 69,424
  • 5
  • 50
  • 86