0

Is there a way to place a completion handler in the parent UIViewController that gets called after its child UIViewController dismisses itself?

For my current project a UIViewController might have to create and present a child UIViewController to get missing data.

My thought was that the completion handler in the present method wouldn't be called till the child UIViewController dismisses itself.

Obviously I was wrong, the completion handler is called immediately after the child UIViewController is presented, yet still exists.

This is an extremely simplified code, just so I can see when the completion handler is being called within the debugger.

override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
   ...determine if data is missing...
   
   if *data is missing* {
      let myUp = UploadInv()
      myUpload.modalPresentationStyle = .fullScreen
      myUpload.modalTransitionStyle = .flipHorizontal
            
      x += 1
      self.present(myUpload, animated: true, completion: { [self] in print ("\(x)");})
   }}
Bartender1382
  • 195
  • 1
  • 10

1 Answers1

0

That completion block is called when your UploadInv was presented. If you want to handle dismissing action from child controller you have to define it inside UploadInv

class UploadInv: UIViewController {
    ....
    var onDismiss: (() -> ())?

    func onDismissAction() {
        self.dismiss(animated: true) {
            onDismiss?()
        }
    }
}

Then use it whenever create an UploadInv instance if needed

let myUp = UploadInv()
myUp.onDismiss = { [weak self] in
    //TODO: - Do something
}
son
  • 1,058
  • 6
  • 7