42

My app keeps crashing, when I set more than one view controller in my app, like below.

[self setViewControllers:_images direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

My images is an array of view controllers.

The app crashes saying the following error. I have no idea where to start.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The number of view controllers provided (9) doesn't match the number required (1) for the requested transition
Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41

3 Answers3

79

As the error says,you are providing more view controllers than needed. You should provide only 1 view controller in an array. Then use page view controller's dataSource methods to provide before and after view controllers properly.

Zen
  • 3,047
  • 1
  • 20
  • 18
  • I wish to know on which view controller my user is on, and an event whenever dragged to move to the next controller. For this I tried using -(void) pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed. But this aint getting called. I have set the delegate and datasource correctly. – Dunes Buggy Jun 27 '13 at 03:03
  • You can get the current view controller instance by getting to the pageViewController's view controller array property as [[pageViewController viewControllers] objectAtIndex:0]; in above delegate. For delegate not calling, check where you set the dataSource and delegate. – Zen Jun 27 '13 at 05:15
  • Sorry, my mistake. I wrote datasource twice, instead of one datasource and delegate in the code. – Dunes Buggy Jun 27 '13 at 05:30
  • 72
    I wonder why the method takes an array if it only allows one element. – Eddie Sullivan Feb 11 '16 at 14:17
  • 24
    @EddieSullivan: page view controllers allow up to two elements, in case its `doubleSided` property is set to `true` and therefore presents two view controllers at a time. – Eric Apr 12 '16 at 12:10
  • 4
    Why not `setViewController` instead? Why Apple is so illiterate? – Pedro Paulo Amorim Jun 01 '17 at 10:34
  • @EddieSullivan as someone above mentioned, in accepts two if two pages are to be displayed at the same time. – David Findlay Apr 18 '18 at 01:57
  • I have set the one view controller in an array, but still facing the issue. https://stackoverflow.com/questions/60501446/the-number-of-view-controllers-provided-0-doesnt-match-the-number-required-1 – HariKarthick Mar 03 '20 at 14:24
1

ok, this is the problem.

In your View Controller, you will have something like this.

private var mainPageView : UIPageViewController!

private var arrayOfVC = [UIViewController]()

now, if you try to set an array of VC (View Controllers) like this:

   self.mainPageView.setViewControllers(arrayOfVC, direction: .forward, animated: true, completion: nil)

There will be an error something like this:

'The number of view controllers provided (9) doesn't match the number required (1) for the requested transition

So, to fix this problem you need to set first VC from an array of VC's

self.mainPageView.setViewControllers([arrayOfVC[0]], direction: .forward, animated: true, completion: nil)

And handle the rest in Page View Delegate and Data Source.

That is all :)

p.s. SWIFT - 5.5

tBug
  • 789
  • 9
  • 13
-7

UIPageViewController in iOS has some bugs. Use UIScrollView + NSArray of UIViewController instead! Maybe this will help. http://weijun.me/post/develop/2015-11-26

  • 1
    I wouldn't say it's buggy as much as it's hard to use and not very customizable. I'd only recommend using it when you want to have a "page turn" animation. If you want a paged scroll view with normal scroll and a pageControl than UIScrollView is easier to use. – moger777 Mar 10 '18 at 15:55