页面视图控制器出错
Getting error with Page View Controller
我不断收到以下错误:
*** 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)
}
这不是页面视图控制器的工作方式。您不传递多个视图控制器,而只传递一个或最多两个。请参阅苹果文档:
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.
我假设您需要基于手势的导航,因此您需要实现页面视图控制器委托和数据源。 google.
上有很多页面视图控制器教程
方法集 setViewControllers
预计只显示第一个视图控制器!像这样尝试:
if let firstVC = myViewControllers?.first {
setViewControllers([firstVC], direction: .forward, animated: true)
}
myViewControllers
是我的 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)
}
这不是页面视图控制器的工作方式。您不传递多个视图控制器,而只传递一个或最多两个。请参阅苹果文档:
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.
我假设您需要基于手势的导航,因此您需要实现页面视图控制器委托和数据源。 google.
上有很多页面视图控制器教程方法集 setViewControllers
预计只显示第一个视图控制器!像这样尝试:
if let firstVC = myViewControllers?.first {
setViewControllers([firstVC], direction: .forward, animated: true)
}
myViewControllers
是我的 UIPageViewController
子类的 属性!
希望对你有用!