Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically reload UIPageViewController after dataSource changed

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?

like image 913
valeriodidonato Avatar asked Aug 09 '12 13:08

valeriodidonato


Video Answer


2 Answers

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

like image 130
Jörn Eyrich Avatar answered Oct 23 '22 02:10

Jörn Eyrich


The main problem was that the webview was intercepting all the user touches and therefore the UiPageViewController wasn't calling the delegate methods. Once set the webview to ignore user touch events everything started to work properly.

like image 29
valeriodidonato Avatar answered Oct 23 '22 02:10

valeriodidonato