gpt4 book ai didi

ios - PageViewController 显示页面索引不正确?

转载 作者:行者123 更新时间:2023-11-29 11:59:05 28 4
gpt4 key购买 nike

我已按照本教程创建 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;
}

试了很多方法都没有解决问题。

帮我找到页面的当前索引。页面索引当前未显示,当我滑动一次时,它一次返回 0,1 索引,下一次滑动时,它显示 2,下一次滑动时,它不返回任何索引

最佳答案

我已经实现了相同的代码并在调试之后了解到 PageViewControllerDataSource 方法被调用了两次。因此,您可以使用 willTransitionToViewControllers 方法代替 viewControllerBeforeViewControllerviewControllerAfterViewController 方法来获取正确的索引。

请引用以下答案,

PageViewController delegate functions called twice

关于ios - PageViewController 显示页面索引不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37803617/

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