PageViewController 显示页面索引不正确?
PageViewController showing the index of the page incorrectly?
我已经按照本教程创建了 PageViewController http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/
问题是我错误地获取了页面,其余的一切都很完美
我需要做一些事情,比如如果页面索引是最后一个索引,那么我已经更改了按钮标签。
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageImages count] == 0) || (index >= [self.pageImages count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
NSLog(@"INDEX : %lu", (unsigned long)index);
return pageContentViewController;
}
- (void)pageViewController:(UIPageViewController *)pageViewCntr didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
NSInteger currentIndex = ((UIViewController *)self.pageViewController.viewControllers.firstObject).view.tag;
if (completed) pageControl.currentPage = currentIndex;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger index = ((PageContentViewController *)viewController).pageIndex;
if (index == NSNotFound || !index) return nil;
NSLog(@"INDEX : %lu", (unsigned long)index);
return [self viewControllerAtIndex:index-1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger index = ((PageContentViewController *)viewController).pageIndex;
if (index == NSNotFound || index == pageCount-1) return nil;
NSLog(@"INDEX : %lu", (unsigned long)index);
return [self viewControllerAtIndex:index+1];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
return [self.pageImages count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
return 0;
}
试了很多方法都没有解决问题
帮我找到页面的当前索引。
页面索引当前不显示,当我滑动一次它 return 一次给我 0,1 索引,下一次滑动它显示 2 而下一次滑动它不 return 我任何指数
我实现了相同的代码,调试后发现 PageViewControllerDataSource
方法被调用了两次。因此,您可以使用 willTransitionToViewControllers
方法代替 viewControllerBeforeViewController
和 viewControllerAfterViewController
方法来获取正确的索引。
请参考以下答案,
PageViewController delegate functions called twice
我已经按照本教程创建了 PageViewController http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ 问题是我错误地获取了页面,其余的一切都很完美
我需要做一些事情,比如如果页面索引是最后一个索引,那么我已经更改了按钮标签。
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageImages count] == 0) || (index >= [self.pageImages count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
NSLog(@"INDEX : %lu", (unsigned long)index);
return pageContentViewController;
}
- (void)pageViewController:(UIPageViewController *)pageViewCntr didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
NSInteger currentIndex = ((UIViewController *)self.pageViewController.viewControllers.firstObject).view.tag;
if (completed) pageControl.currentPage = currentIndex;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger index = ((PageContentViewController *)viewController).pageIndex;
if (index == NSNotFound || !index) return nil;
NSLog(@"INDEX : %lu", (unsigned long)index);
return [self viewControllerAtIndex:index-1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger index = ((PageContentViewController *)viewController).pageIndex;
if (index == NSNotFound || index == pageCount-1) return nil;
NSLog(@"INDEX : %lu", (unsigned long)index);
return [self viewControllerAtIndex:index+1];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
return [self.pageImages count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
return 0;
}
试了很多方法都没有解决问题
帮我找到页面的当前索引。 页面索引当前不显示,当我滑动一次它 return 一次给我 0,1 索引,下一次滑动它显示 2 而下一次滑动它不 return 我任何指数
我实现了相同的代码,调试后发现 PageViewControllerDataSource
方法被调用了两次。因此,您可以使用 willTransitionToViewControllers
方法代替 viewControllerBeforeViewController
和 viewControllerAfterViewController
方法来获取正确的索引。
请参考以下答案,
PageViewController delegate functions called twice