xcode 未调用 UIPageViewController 委托方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22067476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:42:22  来源:igfitidea点击:

UIPageViewController delegate method not called

iosobjective-cxcodeuiviewcontrolleruipageviewcontroller

提问by charles

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.

在我的应用程序中,我有一个RootViewController (UIPageViewController)、一个 FirstController (UIViewController) 和一个 SecondController (UIViewController)。两个UIViewController 中的两个视图在RootViewController 上滚动

In my RootViewController.h:

在我的 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.

不被称为。为什么?有人能帮我吗?先感谢您。

回答by Wilmar

Did you assign rootViewController (whichever controller/object you want to receive the delegate calls) to be the delegate/datasource of the UIPageViewController?

您是否将 rootViewController(无论您想接收委托调用的控制器/对象)指定为 UIPageViewController 的委托/数据源吗?

pageViewController.delegate = rootViewController;
pageViewController.dataSource = rootViewController;

回答by dave

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.

除了上述显而易见的解决方案,如果您遇到此问题,请考虑没有保留 PageViewController 本身的可能性。今天晚上早些时候我犯了一个错误,即实例化一个 PageViewController 并将其视图添加为子视图,但没有保留 pageViewController 本身。

Make sure to retain it either by adding it as a childViewController or assigning it to a strong property.

确保通过将其添加为 childViewController 或将其分配给强大的属性来保留它。

回答by Pawe?

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 :

只是花了一些时间试图弄清楚为什么没有调用 didFinishAnimating。我的代表设置正确,但事实证明,在 Swift 3 中,函数是这样的:

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)

回答by Duncan C

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.

有一个页面视图控制器是它自己的数据源和委托,这很奇怪。我从来没有像你那样尝试过这样做,我不确定它会起作用。我总是设置一个容器视图控制器来管理页面视图控制器。使用故事板,您只需在 IB 中设置一个容器视图并连接一个嵌入转场。然后您将容器视图控制器设为数据源和/或委托。