【问题标题】:UIPageViewController not showing anything but black screen - but doesn't throw errorsUIPageViewController 只显示黑屏 - 但不会抛出错误
【发布时间】:2015-02-26 08:42:29
【问题描述】:

我正在使用 swift 为 iOS 开发一个应用程序。 我已经在我的项目中成功设置了一个 UIPageViewController,所以我知道它是如何工作的。 但是现在我正在尝试设置第二个 PageViewController,但它不起作用 - 它也不会抛出任何错误,它只是直接进入黑屏,甚至不显示任何 segues

到目前为止我所尝试的:

  1. 重置 iOS 模拟器
  2. 重新索引 xcode 文件
  3. 重启xcode
  4. 重启我的机器

这些都没有帮助。

我的代码:

(顺便说一句:这段代码与我工作的 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

}

}

所有打印都显示在控制台中 - 但没有其他任何事情发生。 有谁知道问题可能是什么? 提前谢谢你:)

【问题讨论】:

  • 故事板中有这个视图控制器吗?
  • 是的,绝对是:)
  • 在获取到firstView和secondView后添加断点,检查是否获取到正确的视图控制器而不是nil。
  • 变量和“destinationViewControllers”数组中确实加载了正确的视图控制器
  • 我在这里检查 index == 0 的原因是,如果我不这样做,应用程序将会崩溃 :) 如果我在第一页,那么在它之前不会有任何页面.我只是将所有文件移动到一个单独的项目中,看看它是否可以工作 - 它确实......这很奇怪

标签: ios iphone xcode swift uipageviewcontroller


【解决方案1】:

信不信由你UIPageViewController 类实际上不应该被自己查看。与拆分视图控制器类(但与选项卡控制器类不同)一样,您需要一个主视图来包装页面视图控制器。

您还需要做一些不雅的技巧来加载页面视图控制器而不通过情节提要链接。当你看到有人让它工作的代码时,你可以看出他们必须对 UIViewController 内部了解太多,才能让这么简单的工作..

如果与拆分视图控制器一样,它会在您将其放入情节提要时向您显示标准设置,但不幸的是选项卡视图、拆分视图和页面视图的行为都非常不同.

【讨论】:

  • 你能提供“有人让它工作的代码”的链接吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多