Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

后端 未结 30 3102
一整个雨季
一整个雨季 2020-11-21 23:39

Just started using Xcode 4.5 and I got this error in the console:

Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewCont

相关标签:
30条回答
  • 2020-11-21 23:55

    I had similar issue on Swift 4.2 but my view was not presented from the view cycle. I found that I had multiple segue to be presented at same time. So I used dispatchAsyncAfter.

    func updateView() {
    
     DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
    
    // for programmatically presenting view controller 
    // present(viewController, animated: true, completion: nil)
    
    //For Story board segue. you will also have to setup prepare segue for this to work. 
     self?.performSegue(withIdentifier: "Identifier", sender: nil)
      }
    }
    
    0 讨论(0)
  • 2020-11-21 23:55

    I had this issue, and the root cause was subscribing to a button click handler (TouchUpInside) multiple times.

    It was subscribing in ViewWillAppear, which was being called multiple times since we had added navigation to go to another controller, and then unwind back to it.

    0 讨论(0)
  • 2020-11-21 23:55

    Sadly, the accepted solution did not work for my case. I was trying to navigate to a new View Controller right after unwind from another View Controller.

    I found a solution by using a flag to indicate which unwind segue was called.

    @IBAction func unwindFromAuthenticationWithSegue(segue: UIStoryboardSegue) {
        self.shouldSegueToMainTabBar = true
    }
    
    @IBAction func unwindFromForgetPasswordWithSegue(segue: UIStoryboardSegue) {
        self.shouldSegueToLogin = true
    }
    

    Then present the wanted VC with present(_ viewControllerToPresent: UIViewController)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if self.shouldSegueToMainTabBar {
            let mainTabBarController = storyboard.instantiateViewController(withIdentifier: "mainTabBarVC") as! MainTabBarController
            self.present(mainTabBarController, animated: true)
            self.shouldSegueToMainTabBar = false
        }
        if self.shouldSegueToLogin {
            let loginController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LogInViewController
            self.present(loginController, animated: true)
            self.shouldSegueToLogin = false
        }
    }
    

    Basically, the above code will let me catch the unwind from login/SignUp VC and navigate to the dashboard, or catch the unwind action from forget password VC and navigate to the login page.

    0 讨论(0)
  • 2020-11-21 23:56

    I had the same problem. I had to embed a navigation controller and present the controller through it. Below is the sample code.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        UIImagePickerController *cameraView = [[UIImagePickerController alloc]init];
        [cameraView setSourceType:UIImagePickerControllerSourceTypeCamera];
        [cameraView setShowsCameraControls:NO];
    
        UIView *cameraOverlay = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 1024)];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"someImage"]];
        [imageView setFrame:CGRectMake(0, 0, 768, 1024)];
        [cameraOverlay addSubview:imageView];
    
        [cameraView setCameraOverlayView:imageView];
    
        [self.navigationController presentViewController:cameraView animated:NO completion:nil];
    //    [self presentViewController:cameraView animated:NO completion:nil]; //this will cause view is not in the window hierarchy error
    
    }
    
    0 讨论(0)
  • 2020-11-21 23:57

    I fixed it by moving the start() function inside the dismiss completion block:

    self.tabBarController.dismiss(animated: false) {
      self.start()
    }
    

    Start contains two calls to self.present() one for a UINavigationController and another one for a UIImagePickerController.

    That fixed it for me.

    0 讨论(0)
  • 2020-11-21 23:57

    I fixed this error with storing top most viewcontroller into constant which is found within while cycle over rootViewController:

    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        topController.present(controller, animated: false, completion: nil)
        // topController should now be your topmost view controller
    }
    
    0 讨论(0)
提交回复
热议问题