问题
In my application I have a RootViewController (UIPageViewController), a FirstController (UIViewController) and a SecondController (UIViewController). The two views inside the two UIViewControllers scroll over the RootViewController.
In my RootViewController.h:
@interface RootController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
But when I scroll between different views delegate methods like:
-(void) pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
are not called. Why? Can someone help me? Thank you in advance.
回答1:
Did you assign rootViewController (whichever controller/object you want to receive the delegate calls) to be the delegate/datasource of the UIPageViewController?
pageViewController.delegate = rootViewController;
pageViewController.dataSource = rootViewController;
回答2:
In addition to the obvious solution above, if you are encountering this issue please consider the possibility that the PageViewController itself is not being retained. I made the mistake earlier this evening of instantiating a PageViewController and adding its view as a subView but not retaining the pageViewController itself.
Make sure to retain it either by adding it as a childViewController or assigning it to a strong property.
回答3:
How did you connect the page view controller's delegate outlet? My guess is that you never connected the delegate link.
It's odd to have a page view controller be it's own data source and delegate. I've never tried to do that, as you are doing, and am not sure it would work. I always set up a container view controller that manages the page view controller. With storyboards you just set up a container view in IB and connect an embed segue. Then you'd make the container view controller the data source and/or delegate.
回答4:
Just spent some time trying to figure out why didFinishAnimating was not called. My delegates were set right but it turns out that in Swift 3 the function goes like that :
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
Mind the above underscore. The function below won't be called.
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
来源:https://stackoverflow.com/questions/22067476/uipageviewcontroller-delegate-method-not-called