我正在使用swift开发iOS应用程序。 我已经在我的项目中成功设置了一个UIPageViewController,所以我知道它是如何工作的。 但现在我试图设置第二个PageViewController,它只是不起作用 - 它也没有抛出任何错误,它只是直接进入黑屏并且甚至不均匀显示任何segues
到目前为止我尝试过:
这些都没有帮助。
我的代码:
(顺便说一句:这段代码与我工作的UIPageViewController几乎完全相同,唯一的区别在于变量'名称)
import UIKit
class SelectDestinationViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var destinationViewControllers : [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
println("View did appear")
// Do any additional setup after loading the view.
self.delegate = self
self.dataSource = self
let firstView = storyboard?.instantiateViewControllerWithIdentifier("firstView") as FirstDestinationViewController
let secondView = storyboard?.instantiateViewControllerWithIdentifier("secondView") as SecondDestinationViewController
destinationViewControllers = [firstView, secondView]
for var index = 0; index < destinationViewControllers.count; ++index {
println("\(destinationViewControllers[index])")
}
let startingViewController = self.viewControllerAtIndex(0)
let viewControllers: NSArray = [startingViewController]
self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: {(done: Bool) in})
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("View will appear!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(index:NSInteger) -> UIViewController {
return destinationViewControllers[index]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index = find(destinationViewControllers, viewController)!
if (index == 0) || (index == NSNotFound) {
return nil
}
index--
if index == destinationViewControllers.count {
return nil
}
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index = find(destinationViewControllers, viewController)!
if index == NSNotFound {
return nil
}
index++
if index == destinationViewControllers.count {
return nil
}
return self.viewControllerAtIndex(index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return destinationViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
所有打印件都显示在控制台中 - 但没有其他任何事情发生。 有谁知道问题可能是什么? 提前谢谢你:)
答案 0 :(得分:-2)
信不信任UIPageViewController
类实际上不应该单独查看。与拆分视图控制器类一样(但与选项卡控制器类不同),您需要有一个包含页面视图控制器的主视图。
您还需要做一些不优雅的技巧,以便在不通过故事板链接的情况下加载页面视图控制器。当你看到有人让它工作的代码时,你可以告诉他们必须对UIViewController
内部结构有太多的了解才能得到一些如此简单的工作......
如果与分割视图控制器一样,当您将其放入故事板时,它会向您显示标准设置,但不幸的是,标签视图,拆分视图和页面视图的行为方式会有所不同