iosswiftuipageviewcontrolleruiwindow

Assert Window should be nonnil (Swift Error)


When PageController trying to present it's content I'm encountering following message in console:

"[Assert] Window should be nonnil here unless a subclass is mistakenly sending this to a child when no window can be found".

App is not crashing it's just not showing content it's supposed to show in PageController. Code seems fine to me:


class RulesPageViewController: UIPageViewController {

    var rulesDelegate: RulesPageVeiwControllerDelegate?
    
    var pageHeadings = ["1st page","2nd","3rd"]
    
    var currentIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        self.delegate = self
        
        if let startingViewController = contentViewController(at: 0)
        {
            self.setViewControllers([startingViewController], direction: .forward, animated: false, completion: nil)
        }
    }
    

    func contentViewController(at index: Int)-> PageContentViewController?
    {
        if(index < 0 || index >= pageHeadings.count)
        {
            return nil
        }
        if let pageContentViewController = UIStoryboard(name: "GameRules", bundle: nil).instantiateViewController(withIdentifier: "PageContentViewController") as? PageContentViewController{
            pageContentViewController.index = index
            return pageContentViewController
        }
        return nil
    }

}
extension RulesPageViewController: UIPageViewControllerDelegate {
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        
        if completed {
            if let contentViewController = pageViewController.viewControllers?.first as? PageContentViewController
            {
                currentIndex = contentViewController.index
                rulesDelegate?.didUpdatePageIndex(currentIndex: currentIndex)
            }
        }
    }
}

extension RulesPageViewController: UIPageViewControllerDataSource {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = (viewController as! PageContentViewController).index
        index -= 1
        return contentViewController(at: index)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = (viewController as! PageContentViewController).index
        index += 1
        return contentViewController(at: index)
    }
    
    
}

Solution

  • Problem was solved: PageContentViewController was conforming to wrong class