ios - 页面 View Controller 出错

标签 ios swift uipageviewcontroller

我不断收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The number of view controllers provided (0) doesn't match the number required (1) for the requested transition.'

我不确定我在 PageViewController 中做错了什么可能会导致此错误。我也试着按照提供的答案 here .但是,该解决方案对我没有帮助,因为我不确定我的哪段代码可能导致了问题。我是 Swift 的新手,所以非常感谢任何帮助。

更新: 我发现是 setViewController 导致了这个问题,删除后它就消失了。但是,为什么这会引起问题? 谢谢!

    var pageViewController: UIPageViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("ViewController1") as! UIPageViewController
    self.pageViewController.dataSource = self
    let allViewControllers = [harishStoryboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2, harishStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3, harishStoryboard.instantiateViewControllerWithIdentifier("ViewController4") as! ViewController4, harishStoryboard.instantiateViewControllerWithIdentifier("ViewController5") as! ViewController5, harishStoryboard.instantiateViewControllerWithIdentifier("ViewController6") as! ViewController6, harishStoryboard.instantiateViewControllerWithIdentifier("ViewController7") as! ViewController7]
    let viewControllers = NSArray(object: allViewControllers)
    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: false, completion: nil)
    self.addChildViewController(pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}

最佳答案

这不是页面 View Controller 的工作方式。您不传递多个 View Controller ,而只传递一个或最多两个。请参阅苹果文档:

When defining a page view controller interface, you can provide the content view controllers one at a time (or two at a time, depending upon the spine position and double-sided state) or as-needed using a data source. When providing content view controllers one at a time, you use the setViewControllers:direction:animated:completion: method to set the current content view controllers. To support gesture-based navigation, you must provide your view controllers using a data source object.

我假设您需要基于手势的导航,因此您需要实现页面 View Controller 委托(delegate)和数据源。谷歌上有很多页面 View Controller 教程。

关于ios - 页面 View Controller 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468196/

相关文章:

ios - 按下按钮后,如何使 UIPickerView 从应用程序底部升起?

ios - 如何使用 UIImageJPEGRepresentation(_) -> NSData 压缩的 swift 解压 jpeg?

ios - 如何在页面 View Controller 上显示 float 视频播放器

ios - 使用 UIPageViewController 向左滑动返回第一页

arrays - UIPageControl 未正确更新为 UIPageViewController 中当前页面的索引

ios - 文档提供者的dismissGrantingAccessToURL 在iOS 8.3 中不起作用

ios - 指定要在 TableView 中添加单元格的位置

iOS 应用程序验证错误

ios - 为什么我的约束在 swift/storyboard 中不能正常工作?

swift - 使用协议(protocol)类型属性对结构进行解码/编码