【发布时间】:2015-02-26 08:42:29
【问题描述】:
我正在使用 swift 为 iOS 开发一个应用程序。 我已经在我的项目中成功设置了一个 UIPageViewController,所以我知道它是如何工作的。 但是现在我正在尝试设置第二个 PageViewController,但它不起作用 - 它也不会抛出任何错误,它只是直接进入黑屏,甚至不显示任何 segues
到目前为止我所尝试的:
- 重置 iOS 模拟器
- 重新索引 xcode 文件
- 重启xcode
- 重启我的机器
这些都没有帮助。
我的代码:
(顺便说一句:这段代码与我工作的 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