刷新 UIPageViewController [英] Refresh a UIPageViewController

查看:36
本文介绍了刷新 UIPageViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIPageViewController( 委托)来管理 3 个 UIViewController.要从第二个视图转到第三个视图,我必须点击一个按钮,否则第二个视图是最后一个可见页面.一旦进入第 3 页,用户应该能够通过滑动返回到第 2,但不能通过滑动返回到第 3(该按钮始终是到达第 3 页所必需的).这是代码的一部分:

I have a UIPageViewController (<UIPageViewControllerDataSource> delegate) that manages 3 UIViewController. To go from the second view to the third, i have to tap a button, otherwise the second view is the last visible page. Once on page 3, the user should be able to swipe to get back to 2, but not be able to swipe to return to 3 (the button is always necessary to get to page 3). This is part of the code:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    UIViewController *prevViewController = nil;
    if (viewController == self.P2ViewController) {
        prevViewController = self.P1ViewController;
    }
    if (viewController == self.P3ViewController) {
        [self pageViewController:pageViewController viewControllerAfterViewController:self.P2ViewController]; /*this doesn't work*/
        prevViewController = self.P2ViewController;
    }
    return prevViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    UIViewController *nextViewController = nil;
    if (viewController == self.P1ViewController) {
        nextViewController = self.P2ViewController;
    }
    if (viewController == self.P2ViewController) {
        nextViewController = nil;
    }
    return nextViewController;
}

然后我有一个自定义的 UIViewController 类,其中我有一个 IBAction 并且我传递了带有静态变量和通知的正确 UIViewController.部分代码:

Then i have a custom UIViewController class where i have an IBAction and i pass the correct UIViewController with a static variable and a notification. Part of the code:

-(IBAction)goToPage:(NSNotification *)notification{
    /*some code*/
    [((UIPageViewController *)self.parentViewController) setViewControllers:viewControllersArray direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}

我第一次从第 1 页转到第 2 页时,第二页是最后一页.然后我点击按钮,我用动画正确地转到第三页(UIPageViewController 中的动画错误"对我很有用).然后第三个视图是最后一个,我可以返回到第二个视图,但是当我返回时没有调用 viewControllerAfterViewController: 方法,我仍然可以转到第三个页面.

The first time i go from page 1 to 2, the second page is the last one. Then i tap the button and i go to the third page correctly with the animation (the "animated bug" in UIPageViewController works good for me). Then the third view is the last one and i can go back to the second view, but when i go back no viewControllerAfterViewController: method is called and i can still go to the third page.

相反,我想返回到第二页,再次将第二页作为最后一页,每次返回时从缓存中删除第三页.我已经在第二页的自定义 UIViewController 类中尝试使用此代码:

Instead, i want to go back to the second page leaving the second page as the last one again, deleting the third page from the cache every time i go back. I've tried with this code in my custom UIViewController class for the second page:

-(void)viewWillAppear:(BOOL)animated{
    [((UIPageViewController *)self.parentViewController) setViewControllers:@[myStaticViewController2] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

但它只在我第二次尝试从该页面来回移动时才有效.像我一样再次调用 viewControllerBeforeViewController 中的 viewControllerAfterViewController 也是没用的.我希望我很清楚,我找不到这个问题的解决方案.

but it only works the second time i try to move back or forth from that page. It's also useless calling again the viewControllerAfterViewController inside the viewControllerBeforeViewController like i did. I hope i was clear, i can't find a solution to this problem.

提前致谢

推荐答案

好的 - 我想我找到了让它工作的方法.

OK - think I found a way to make it work.

把它放在 PageViewController 中,并在 .h 文件中添加一个声明:

Put this in PageViewController, and add a declaration to the .h file:

// a button press is causing a move
- (void)moveToThirdController
{
    __weak PageViewController *pc = self;
    [self setViewControllers:@[self.P3ViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL ret)
        {
            // second dispatch may be overkill
            dispatch_async(dispatch_get_main_queue(), ^
                {
                    // it seems setting no wipes the cache
                    [pc setViewControllers:@[pc.P3ViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
                } );
        } ];
}

在你的第二个视图控制器中,我稍微修改了你在那里的内容:

In you second view controller, I slightly modified what you ahd there:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    __weak P2ViewController *pc = self;
    dispatch_async(dispatch_get_main_queue(), ^
        {
            [((UIPageViewController *)self.parentViewController) setViewControllers:@[pc] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
        } );
}

关键是不断尝试让 pageViewController 在第二次调用时移除它的缓存,其中动画为 NO.

The key was to constantly try to get the pageViewController to remove its cache with a second call where animated is NO.

事实证明,这是第一次在此处

It turns out this is a variation on the solution first posted here

这篇关于刷新 UIPageViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆