gpt4 book ai didi

ios - UIPageViewController同时调用viewControllerBeforeViewController和viewControllerAfterViewController

转载 作者:行者123 更新时间:2023-12-02 00:45:29 26 4
gpt4 key购买 nike

我在 stackoverflow 上看到过类似的问题(例如 herehere ),但没有一个有明确的答案。我关注了this tutorial但为我想要显示的每个页面创建了一个单独的 UIViewController 文件,其中包含它自己的 .xib 。第一次滑动时,viewControllerBeforeViewControllerviewControllerAfterViewController 都会被调用。本教程使用这两种方法来递增或递减 pageIndex。所以我最终看到的是 pageIndex 初始化为 0,然后在调用 viewControllerAfterViewController 时设置为 1,然后在 viewControllerBeforeViewController 调用时立即设置为 0叫。最终,这会导致页面显示不正确。

最佳答案

这是我解决这个问题的方法。

在每个页面 View Controller 中,我创建了一个名为 indexreadonly 属性,我在其实现中将其指定为相关值(例如 1、2 或 3) )。然后我这样做了:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger pageIndex = 0;
if ([viewController isKindOfClass:[MainMenuPage0ViewController class]]) {
MainMenuPage0ViewController *mainMenuPage0ViewController = (MainMenuPage0ViewController *)viewController;
pageIndex = mainMenuPage0ViewController.index;
pageIndex--;
} else if ([viewController isKindOfClass:[MainMenuPage1ViewController class]]) {
MainMenuPage1ViewController *mainMenuPage1ViewController = (MainMenuPage1ViewController *)viewController;
pageIndex = mainMenuPage1ViewController.index;
pageIndex--;
} else if ([viewController isKindOfClass:[MainMenuPage2ViewController class]]) {
MainMenuPage2ViewController *mainMenuPage2ViewController = (MainMenuPage2ViewController *)viewController;
pageIndex = mainMenuPage2ViewController.index;
pageIndex--;
}

if (pageIndex < 0) {
return nil;
}

return [self viewControllerAtIndex:pageIndex];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger pageIndex = 0;
if ([viewController isKindOfClass:[MainMenuPage0ViewController class]]) {
MainMenuPage0ViewController *mainMenuPage0ViewController = (MainMenuPage0ViewController *)viewController;
pageIndex = mainMenuPage0ViewController.index;
pageIndex++;
} else if ([viewController isKindOfClass:[MainMenuPage1ViewController class]]) {
MainMenuPage1ViewController *mainMenuPage1ViewController = (MainMenuPage1ViewController *)viewController;
pageIndex = mainMenuPage1ViewController.index;
pageIndex++;
} else if ([viewController isKindOfClass:[MainMenuPage2ViewController class]]) {
MainMenuPage2ViewController *mainMenuPage2ViewController = (MainMenuPage2ViewController *)viewController;
pageIndex = mainMenuPage2ViewController.index;
pageIndex++;
}

if (pageIndex == 3) {
return nil;
}

return [self viewControllerAtIndex:pageIndex];
}

关于ios - UIPageViewController同时调用viewControllerBeforeViewController和viewControllerAfterViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com