0

In iOS 13, viewWillAppear is not called when dismissing view controller. As a workaround, it is mentioned to override UIAdaptivePresentationControllerDelegate delegate, but it's not working for me. What am I doing wrong?

 func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "MyVC" {
       let destination = segue.destination as! MyViewController
        destination.presentationController?.delegate = self
    } 
  }

And then,

func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
     resumePipeline() //<--Does not get called
}
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • Current view controller that is executing the segue. – Deepak Sharma Aug 11 '19 at 15:40
  • Ok I have a main view controller(called MainViewController) and I present MyViewController from it via Segue. The segue is defined in Storyboard and is set to "Show" (animated). In prepareForSegue, I am trying to set delegate of presentationController as shown above in code. How do I get notified when MyViewController is dismissed? One way is defining a delegate but then I have many view controllers like MyViewController. I relied on viewWillAppear before but now it's not possible. – Deepak Sharma Aug 11 '19 at 15:52
  • 1
    Well, `presentationControllerDidDismiss` is only called if the user dismisses the presented view controller _by dragging_ (down). If your presenting view controller needs to be notified if something else happens (e.g. the user taps a button to dismiss inside the presented view), you'll need another mechanism. – matt Aug 11 '19 at 16:38
  • uffff, does that mean defining a delegate, and explicitly invoking actions when a view controller is presented and dismissed? – Deepak Sharma Aug 11 '19 at 16:41
  • I can't tell you what it means for you. If you don't like the new style, don't use it. If you want the new style, adapt to it. – matt Aug 11 '19 at 16:42
  • I didn't find any of matt's comments helpful. You're not listening to the OP. I too have this issue. Who's presentationController do you assign a delegate to? It's not about a method not firing, it's about the correct view controller getting the assignment. – horseshoe7 Mar 21 '22 at 09:52
  • So it's the controller that's involved in the dismissal. If your VC is embedded in a navigation controller, then you have to set the presentationController delegate of the navigation controller. – horseshoe7 Mar 21 '22 at 10:13

1 Answers1

4

What am I doing wrong?

You're probably assuming that presentationControllerDidDismiss is always called when the dismissal takes place. That's a false assumption. It is called when the user drags down on the presented view to dismiss it.

You need to think of the presented view controller as if it were a popover. It isn't completely replacing the presenting view controller's view; it just covers it partially. So there is no viewDidAppear call, because the main view never disappeared.

Either you need to go back to forcing your presented view controller to be fullScreen or you need to adapt your architecture to work with the new style of presented view controller.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok, how do I force it to be fullScreen? I tried setting modalPresentationStyle as fullScreen and segue type as 'Present Modally'. But no effect. Something that works in iOS to force the view controller to be presented fullScreen will be helpful (provided it invokes viewWillAppear). – Deepak Sharma Aug 11 '19 at 17:42
  • Very strange, I restored my iPhone XR back to iOS 12 and then again to iOS 13 and problem went away! Now viewWillAppear is getting called. No idea why. – Deepak Sharma Aug 11 '19 at 18:05
  • No, I am building against iOS 13. The things is defining a segue in Storyboard with kind "Show (e.g.) Push" does call viewWillDisappear. But if the segue is defined as "Present Modally", then the story is different. In the segues I defined Present Modally, viewWillDisappear/appear are not called until I set it to be fullScreen. But earlier before resetting the phone, even Show segues had the same issue! – Deepak Sharma Aug 12 '19 at 06:34