View not in hierarchy after presenting a ViewController

I’ve found a solution, not necessarily in the best one, but it’s working. In the scannerVC I dismiss it after it has grabbed the qrCode, then I make the call to the delegate in ViewDidDIsappear.

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        captureSession.stopRunning()

        if let metadataObject = metadataObjects.first {
            guard let...
        }

      dismiss(animated: true)
      }
  
  override func viewDidDisappear(_ animated: Bool) {
    let displayAlert = ViewController()
    let scanner = ScannerViewController()
    scanner.delegate = displayAlert
    scanner.codeWasImported()
  }

In the displayAlert method in VC1 I have this, which successfully presents the alert, from testAlert(). One other thing I did was change the segue, in storyboard, from button-ScannerVC to ViewController-ScannerVC.

if let view = UIApplication.shared.windows[0].rootViewController as? UINavigationController {
      view.popToRootViewController(animated: true)
      if let vc = view.visibleViewController as? ViewController{
        vc.testAlert()
      }
      }