UIPageViewController viewControllerAfterViewController 从未调用过
UIPageViewController viewControllerAfterViewController never called
我有一个 UIPageViewController,我设置了一个数组作为数据源。我看到第一页很好,演示文稿计数点显示正确,但是当我尝试将 拖到下一页时,没有任何反应。所以我在 viewControllerAfterViewController
和 Before
方法中设置了一些断点,并且 它们永远不会在 上被调用。 presentationCountForPageViewController
的其他数据源方法调用正常。
知道我错过了什么吗?我一直在逐行查看我在网上找到的示例,但找不到任何内容。
- (void)viewDidLoad
{
[super viewDidLoad];
UIPageViewController *pageController = self.pageController;
pageController.dataSource = self;
UIChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[initialViewController];
[pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:pageController];
[self.view addSubview:pageController.view];
[pageController didMoveToParentViewController:self];
}
- (UIPageViewController *)pageController
{
return !_pageController ? _pageController =
({
UIPageViewController *value = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
value.view.frame = self.view.bounds;
value;
}) : _pageController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pa geViewController viewControllerBeforeViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
return (index == 0 ) ? nil : [self viewControllerAtIndex:--index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
index++;
return (index == self.myDataSourceArray.count) ? nil : [self viewControllerAtIndex:index];
}
- (UIChildViewController *)viewControllerAtIndex:(NSUInteger)index
{
UIChildViewController *childViewController = [UIChildViewController new];
childViewController.view.frame = self.pageController.view.bounds;
childViewController.index = index;
...
return childViewController;
}
- (NSInteger)presentationCountForPageViewController: (UIPageViewController *)pageViewController
{
// The number of items reflected in the page indicator.
return self.myDataSourceArray.count;
}
- (NSInteger)presentationIndexForPageViewController:(UI PageViewController *)pageViewController
{
// The selected item reflected in the page indicator.
return 0;
}
问题是在包含封装 UIPageViewController 的 UIViewController 子类时,我忘记在父视图控制器的 viewDidLoad
中包含 addChildViewController
和 didMoveToParentViewController
[self addChildViewController:myViewController];
[self.view addSubview:myViewController.view];
[myViewControllerController didMoveToParentViewController:self];
已解决问题。
我有一个 UIPageViewController,我设置了一个数组作为数据源。我看到第一页很好,演示文稿计数点显示正确,但是当我尝试将 拖到下一页时,没有任何反应。所以我在 viewControllerAfterViewController
和 Before
方法中设置了一些断点,并且 它们永远不会在 上被调用。 presentationCountForPageViewController
的其他数据源方法调用正常。
知道我错过了什么吗?我一直在逐行查看我在网上找到的示例,但找不到任何内容。
- (void)viewDidLoad
{
[super viewDidLoad];
UIPageViewController *pageController = self.pageController;
pageController.dataSource = self;
UIChildViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[initialViewController];
[pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:pageController];
[self.view addSubview:pageController.view];
[pageController didMoveToParentViewController:self];
}
- (UIPageViewController *)pageController
{
return !_pageController ? _pageController =
({
UIPageViewController *value = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
value.view.frame = self.view.bounds;
value;
}) : _pageController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pa geViewController viewControllerBeforeViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
return (index == 0 ) ? nil : [self viewControllerAtIndex:--index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIChildViewController *)viewController
{
NSUInteger index = viewController.index;
index++;
return (index == self.myDataSourceArray.count) ? nil : [self viewControllerAtIndex:index];
}
- (UIChildViewController *)viewControllerAtIndex:(NSUInteger)index
{
UIChildViewController *childViewController = [UIChildViewController new];
childViewController.view.frame = self.pageController.view.bounds;
childViewController.index = index;
...
return childViewController;
}
- (NSInteger)presentationCountForPageViewController: (UIPageViewController *)pageViewController
{
// The number of items reflected in the page indicator.
return self.myDataSourceArray.count;
}
- (NSInteger)presentationIndexForPageViewController:(UI PageViewController *)pageViewController
{
// The selected item reflected in the page indicator.
return 0;
}
问题是在包含封装 UIPageViewController 的 UIViewController 子类时,我忘记在父视图控制器的 viewDidLoad
中包含 addChildViewController
和 didMoveToParentViewController
[self addChildViewController:myViewController];
[self.view addSubview:myViewController.view];
[myViewControllerController didMoveToParentViewController:self];
已解决问题。