我发现了一些关于如何跳转到特定页面的问题,但我注意到跳转的另一个问题,似乎没有一个答案承认.

不必详细介绍我的iOS应用程序(类似于分页日历),以下是我的体验.我声明了一个UIPageViewController,设置了当前的视图控制器,并实现了一个数据源.

// end of the init method
        pageViewController = [[UIPageViewController alloc] 
        initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                        options:nil];
        pageViewController.dataSource = self;
        [self jumpToDay:0];
}

//...

- (void)jumpToDay:(NSInteger)day {
        UIViewController *controller = [self dequeuePreviousDayViewControllerWithDaysBack:day];
        [pageViewController setViewControllers:@[controller]
                                    direction:UIPageViewControllerNavigationDirectionForward
                                     animated:YES
                                   completion:nil];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        NSInteger days = ((THDayViewController *)viewController).daysAgo;
        return [self dequeuePreviousDayViewControllerWithDaysBack:days + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
        NSInteger days = ((THDayViewController *)viewController).daysAgo;
        return [self dequeuePreviousDayViewControllerWithDaysBack:days - 1];
}

- (UIViewController *)dequeuePreviousDayViewControllerWithDaysBack:(NSInteger)days {
        return [[THPreviousDayViewController alloc] initWithDaysAgo:days];
}

编辑注:我为出列方法添加了简化代码.即使使用这种亵渎神明的实现,我在页面顺序方面也有完全相同的问题.

初始化工作正常.增量分页也可以正常工作.问题是,如果我再打jumpToDay,订单就会变得混乱.

如果用户在第5天跳到第1天,向左滚动将再次显示第5天,而不是相应的第0天.这似乎与UIPageViewController如何保存对附近页面的引用有关,但我找不到任何对方法的引用,这些引用会迫使它刷新缓存.

有什么 idea 吗?

推荐答案

马特·纽伯格(Matt Neuburg)记录了这个确切的问题,我发现他的解决方案感觉比目前公认的答案好一点.这种解决方案效果很好,它有一个负面的副作用,即在图像之前/之后设置动画,然后用所需的页面替换该页面.我觉得这是一种奇怪的用户体验,马特的解决方案解决了这个问题.

__weak UIPageViewController* pvcw = pvc;
[pvc setViewControllers:@[page]
              direction:UIPageViewControllerNavigationDirectionForward
               animated:YES completion:^(BOOL finished) {
                   UIPageViewController* pvcs = pvcw;
                   if (!pvcs) return;
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [pvcs setViewControllers:@[page]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO completion:nil];
                   });
               }];

Objective-c相关问答推荐

在从 UIViewController 调用的非保留完成中引用 self 时,weakSelf/strongSelf 舞蹈真的有必要吗?

self.ivar 和 ivar 之间的区别?

将 CSS 插入 UIWebView / WKWebView 中加载的 HTML

在 viewDidAppear 之前无法正确设置框架

在 UILabel.attributedText *not* 蓝色和 *not* 下划线

从 NSString 创建 SHA1 哈希

如何获取 AppIcon 的 UIImage?

NSView 的边界与框架

Interface Builder 中的 IBOutletCollection 集排序

使用 UIView animateWithDuration 进行动画处理时无法 UIButton

如何隐藏/显示导航栏中的右键

以编程方式创建 UITableView

使用 DatePicker 展开和折叠 UITableViewCells

iPhone 键盘、完成按钮和 resignFirstResponder

Core Data使用原始数据类型的标量属性复选框

从 iPhone 设备查找当前国家

UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

请求访问相机胶卷的权限

使用 Objective-C 发布多部分/表单数据

Objective C 消息分发机制