5

I have single view application that also presents a custom popup which I have made using another viewController. The popup viewController has the following assigned:

 presentation: Over current context

enter image description here

when it is presented. When I dismiss this popup viewController to show the first viewController, I would like to know what function gets called so that I can do more stuff within that function. I have tested the following functions below but none of them are called when I have dismissed the popup to reveal the first viewController.

class firstViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        print("viewWillAppear PdfViewController...")   
    }


    override func viewDidLoad() {
        super.viewDidLoad()
    
        print("viewDidLoad PdfViewController ...")
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
jamesMcKey
  • 481
  • 5
  • 28
  • You should call `super.viewWillAppear(animated)` inside `viewWillAppear` always. – Rakesha Shastri Aug 28 '18 at 13:25
  • @RakeshaShastri - I have done what you suggested but it does not get called stil ....I have updated the question with the addition – jamesMcKey Aug 28 '18 at 13:27
  • @jamesMcKey presentation over current context means that the child controller will be shown on the top of parent controller. Parent controller won't be disappeared. So that's why viewWillAppear and viewDidAppear for parent controller won't be called. You can create your own custom presentation style for getting callbacks. See here : https://stackoverflow.com/questions/30474034/view-controller-lifecycle-on-uimodalpresentationovercurrentcontext – Meeran Tariq Aug 28 '18 at 13:38
  • It was a tip for something you missed which probably wasn't causing your issue, but was really important. @Sh_Khan has given you the right answer. Look up delegates and implement his answer. – Rakesha Shastri Aug 28 '18 at 13:38

1 Answers1

5

This depends on the style overCurrentContext doesn't call viewWillAppear/viewDidAppear , for example fullScreen does , look here for all possible styles

https://developer.apple.com/documentation/uikit/uimodalpresentationstyle

if the selected one doesn't call the method then implement delegate for that when you dismiss the modal

//

protocol DimissManager {
  func vcDismissed()
}
class FirstVc:UIViewController,DimissManager {

 func vcDismissed() {
   print("Dismissed")
 }
 func showSecond() {
    let sec = storyboard
    sec.delegate = self ...... // note this assign may be in prepareForSegue if you use segues  
    self.present(sec.......
 }
}
class SecondVc:UIViewController{
   var delegate:DimissManager?

  @IBAction func dismiss(_ sender:UIButton) {
    delegate?.vcDismissed()
    self.dismiss(animated:true,completion:nil)
  }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87