【问题标题】:What should I return in presentationIndexForPageViewController: for my UIPageViewControllerDataSource?我应该在 PresentationIndexForPageViewController: 中为我的 UIPageViewControllerDataSource 返回什么?
【发布时间】:2015-04-20 20:35:53
【问题描述】:

presentationIndexForPageViewController: 返回值的文档说:

返回要反映在页面指示器中的所选项目的索引。

但是,这是模糊的。当用户滚动浏览页面视图控制器时,它会调用此方法并期望正确的索引吗?

此外,无法保证何时 pageViewController:viewControllerBeforeViewController:pageViewController:viewControllerAfterViewController:。文档只是提到:

[An] 对象 [provides] 视图控制器根据需要为页面视图控制器提供,以响应导航手势。

事实上,我已经看到在某些情况下会发生缓存。例如,看起来只有当您从它向前导航两个页面时,视图控制器才会被释放。否则它想把它保存在缓存中,以防用户在页面视图控制器中向后退。

这是否意味着我需要以一致的方式通过注册为UIPageViewControllerDelegate 然后constantly updating this value 来了解当前正在显示的页面?

【问题讨论】:

    标签: ios objective-c uipageviewcontroller


    【解决方案1】:

    关于 presentationCountForPageViewController:presentationIndexForPageViewController:,文档指出:

    这两个方法都是在调用setViewControllers:direction:animated:completion: 方法之后调用的。在手势驱动导航之后,这些方法都不会被调用。索引会自动更新,并且视图控制器的数量预计会保持不变。

    因此,看起来我们只需要在调用setViewControllers:direction:animated:completion: 后立即返回一个有效值。

    每当我实现数据源时,我都会创建一个辅助方法showViewControllerAtIndex:animated:,并跟踪要在属性presentationPageIndex 中返回的值:

    @property (nonatomic, assign) NSInteger presentationPageIndex;
    @property (nonatomic, strong) NSArray *viewControllers; // customize this as needed
    
    // ...
    
    - (void)showViewControllerAtIndex:(NSUInteger)index animated:(BOOL)animated {
        self.presentationPageIndex = index;
        [self.pageViewController setViewControllers:@[self.viewControllers[index]] direction:UIPageViewControllerNavigationDirectionForward animated:animated completion:nil];
    }
    
    #pragma mark - UIPageViewControllerDataSource
    
    - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
        return self.presentationPageIndex;
    }
    

    然后您可以使用此方法显示正确的视图控制器并让选定的索引显示正确的值:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self showViewControllerAtIndex:0 animated:NO];
    }
    
    - (IBAction)buttonTapped {
        [self showViewControllerAtIndex:3 animated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2015-11-04
      • 2015-01-07
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多