Dynamically reload UIPageViewController after dataSource changed

ε祈祈猫儿з 提交于 2019-11-30 05:21:24

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/11884816/dynamically-reload-uipageviewcontroller-after-datasource-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!