0
votes

So I'm attempting to a cue a segue from a model VC to run on the root VC however when I attempt to do so it gives me this error - Warning: Attempt to present vc whose view is not in the window hierarchy. I tried the viewWIllAppear() but that also doesn't work because my VC only covers half the root vc's view as it is a side menu. However, after cueing a segue directly from the root VC it works fine.

this is the code that I have rn:

dismiss(animated: true) {

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
    vc?.performSegue(withIdentifier: "ToSubmition", sender: self)

}
4
Your main VC is embedded to the UINavigationController?Mannopson
You might be calling it from the viewdidload method if so it'll give this warningDatForis
@Mannopson yah in storyboard its set to model with that code to my NavigationControllerSina
Post the notification to your MainVC whenever dismiss action is completed.Mannopson

4 Answers

0
votes

Try dismissing the view controller first:

dismiss(animated: true) {

    self.dismiss(animated: true) { 
        self.performSegue(withIdentifier: "ToSubmition", sender: self)
    }
}
0
votes

I think you're trying to present a VC from your side menu, which of course can't be done as the view is not in the window's hierarchy. What you can do is to make use of NSNotificationCenter to post a notification key once the function is called and dismiss your side menu. Then in your Main VC, add an observer and create a function to perform your intended segue.

You can read from the Apple API reference on how to implement NSNotification Center.

0
votes
  • Completion handlers always uses the background thread, to do UI updates or something like yours use main thread

    dismiss(animated: true) {
      DispatchQueue.main.async {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
        vc?.performSegue(withIdentifier: "ToSubmition", sender: self)
      }
    }
    
  • If it fails use dispatch_after and set some delay before performing segue.

    dismiss(animated: true) {
      let mainQueue = DispatchQueue.main
      let deadline = DispatchTime.now() + .seconds(5)
      mainQueue.asyncAfter(deadline: deadline) {
         // ...perform segue
      }
    }
    
  • It is a blind shot give it a try.
0
votes

Ok so I figured it out. you couldn't call another view controller by creating an instance. instead I just called the present VC at the time

dismiss(animated: true) {
        if let mainView = UIApplication.shared.keyWindow?.rootViewController as? MainViewController {
            mainView.ToSubmition()
        }
    }