Dynamically reload UIPageViewController after dataSource changed
Asked Answered
A

2

5

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?

Axiom answered 9/8, 2012 at 13:43 Comment(0)
A
1

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.

Axiom answered 22/8, 2012 at 21:59 Comment(0)
F
11

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

Ferroconcrete answered 9/8, 2012 at 14:43 Comment(1)
does viewControllerAtIndex: access the elements of self.controllers or something else?Ibnrushd
A
1

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.

Axiom answered 22/8, 2012 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.