Dynamically reload UIPageViewController after dataSource changed

valeriodidonato picture valeriodidonato · Aug 9, 2012 · Viewed 11.3k times · Source

so I'm trying to create an ibooks-like reader and I need to update the contents of the UIPageViewController dynamically after I get the data from the webservice. For a long series of reasons I have to instantiate it at the beginning and then update it when the data comes.

I'm creating it like this:

NSDictionary *options =
[NSDictionary dictionaryWithObject:
 [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                            forKey: UIPageViewControllerOptionSpineLocationKey];

self.pageController = [[UIPageViewController alloc]
                       initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                       navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                       options: options];

self.pageController.dataSource = self;
self.pageController.delegate = self;

[[self.pageController view] setFrame:[[self view] bounds]];

MovellaContentViewController *initialViewController = [self viewControllerAtIndex:0];

self.controllers = [NSArray arrayWithObject:initialViewController];

[self.pageController setViewControllers:self.controllers
                              direction:UIPageViewControllerNavigationDirectionForward
                               animated:NO
                             completion:nil];

[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];

and then I just set self.controllers = newControllersArray;

I'm looking for something like reloadData for a UITableViewController, is there such a thing for UIPageViewController?

Answer

Jörn Eyrich picture Jörn Eyrich · Aug 9, 2012

You have to send setViewControllers:direction:animated:completion: to the pageController again and give it the first view controller to display:

[self.pageController setViewControllers:[NSArray arrayWithObject:
                                            [self viewControllerAtIndex:0]
                                        ]
                              direction:UIPageViewControllerNavigationDirectionForward
                               animated:NO
                             completion:nil];

If viewControllerAtIndex:does not access self.controllers as I at first assumed, it should probably rather be

[self.pageController setViewControllers:[NSArray arrayWithObject:
                                            [self.controllers objectAtIndex:0]
                                        ]
                              direction:UIPageViewControllerNavigationDirectionForward
                               animated:NO
                             completion:nil];

instead