Swift 动态转换失败 UIPageViewController [英] Swift dynamic cast failed UIPageViewController
问题描述
我试图将我的 window.rootViewController 转换为 UIPageViewController,但是每当我访问我定义的类属性时,它都会因 swift 动态转换失败
而崩溃.我的故事板有一个 UIPageViewController 是初始场景.
I am trying to cast my window.rootViewController as a UIPageViewController but anytime I access the class property which I defined it blows up with swift dynamic cast failed
. My storyboard has a UIPageViewController is the initial scene.
PageViewController 只是 UIPageViewController 的一个子类
PageViewController is just a subclass of UIPageViewController
class AppDelegate: UIResponder, UIApplicationDelegate, UIPageViewControllerDataSource {
var window: UIWindow!
var pageViewController: PageViewController {
return window.rootViewController as PageViewController
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
pageViewController.dataSource = self
return true
}
}
推荐答案
主要问题实际上是因为我正在继承 UIPageViewController 我必须将自定义类添加到我的故事板内的 ViewController.起初我想,由于 PageViewController 是一个 UIPageViewController,而且我只是将 rootViewController 转换为一个 PageViewController,它应该可以工作.通过添加自定义类,它解决了我的问题.
The main issue actually was because I was subclassing UIPageViewController I had to add the custom class to the ViewController inside my storyboard. At first I figured that since PageViewController is a UIPageViewController, and I am just casting the rootViewController to a PageViewController, it should've of worked. By adding the custom class it fixed my problem.
我还通过非子类化进行了测试,它无需指定自定义类即可工作.
I also tested by not subclassing, it worked with out specifying a custom class.
这篇关于Swift 动态转换失败 UIPageViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!