断言 Window 应该是非零(Swift 错误)
Assert Window should be nonnil (Swift Error)
当 PageController 尝试显示其内容时,我在控制台中遇到以下消息:
"[Assert] Window should be nonnil here unless a subclass is mistakenly sending this to a child when no window can be found".
应用程序没有崩溃,只是没有显示它应该在 PageController
中显示的内容。代码对我来说似乎没问题:
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)
}
}
问题已解决:页面 ContentViewController 符合错误 class
当 PageController 尝试显示其内容时,我在控制台中遇到以下消息:
"[Assert] Window should be nonnil here unless a subclass is mistakenly sending this to a child when no window can be found".
应用程序没有崩溃,只是没有显示它应该在 PageController
中显示的内容。代码对我来说似乎没问题:
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)
}
}
问题已解决:页面 ContentViewController 符合错误 class