30

在过去的几天里,我一直在努力解决这个问题,经过所有这些杂耍之后,我发现我所需要的只是数据源方法中的当前索引,以使用当前可见页码进行更新

我有这个UIPageViewController数据源方法,我需要使用当前索引来获取委托方法的当前可见页面previousViewControllers transitionCompleted:(BOOL)completed

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
   viewControllerAfterViewController:(UIViewController *)viewController { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];

    NSURL *pdfurl = [NSURL fileURLWithPath:path];
    PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);

    contentViewController = [[ContentViewController alloc] initWithPDFAtPath:path];

    currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];

    if (currentIndex == totalPages - 1) {  
        return nil;
    }

    contentViewController.page = [modelArray objectAtIndex:currentIndex + 1];

    return contentViewController;
}

我对如何将当前索引语句从数据源方法写入委托方法以更新当前可见页面感到困惑。

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

    if (!completed)
    {
        return;
    }

    //currentIndex = [[self.pageViewController.viewController lastObject]];  
    currentIndex = [self indexOfObject:contentViewController];

    [self displaycurrentIndex:currentIndex];
    //self.pageControl.currentPage = currentIndex;
}

我该如何纠正?

4

12 回答 12

28

It looks like you should be able to get the current index with:

ContentViewController *viewController = [self.pageViewController.viewControllers lastObject];
currentIndex = [modelArray indexOfObject:[viewController page]];
于 2013-07-20T22:09:37.850 回答
20

快速:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
        let pageContentViewController = pageViewController.viewControllers![0] as! ViewController
        let index = pageContentViewController.pageIndex

}
于 2015-10-09T23:17:55.387 回答
8

如果您实现 ModelController(如 Apple 示例),请使用它的方法indexOfViewController

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    myDataViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];
    NSInteger currentIndex = [_modelController indexOfViewController:currentView];
    [self displayPageNumber:currentIndex];
}
于 2014-07-21T21:37:10.163 回答
5

我的方法很简单:

  1. 当页面(控制器)被实例化时,我传入页面索引,此时这是已知的。

  2. 然后在下面的委托调用中,如果转换完成,我获取当前显示的控制器并将其转换为我的自定义页面类,然后使用关联的页面索引获取和更新 currentPage。

为了简单起见,我设置了数据源并将其委托为WalkThroughController嵌入页面视图控制器的数据源。

extension WalkThroughController: UIPageViewControllerDelegate {

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        guard finished, let currentPageController = pageViewController.viewControllers?.last as? WalkThroughPageViewController else {
            return
        }

        self.currentPage = currentPageController.pageIndex
    }

}

要点是在数据源中创建页面时设置页面的页面索引。使用这种方法,以后检索它非常简单。

于 2019-02-13T15:30:28.707 回答
5

我在 pageViewController 上遇到了同样的问题,每次我刷我的 pageViewController 时,它都会调用我的委托两次并且无法找到问题。而且我的数组是动态的,所以我想要 pageController 的正确索引值。首先设置 pageViewController 的委托。像这样 :-

self.pageViewController.delegate = self;

然后将此委托方法添加到您的类

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (completed) {
    NSInteger currentIndex = ((PageContentViewController *)self.pageViewController.viewControllers.firstObject).pageIndex;

    NSLog(@"%ld",(long)currentIndex);
}
}

PageContentViewController 是您显示 pageViewController 数据的类。我的 PageContentViewController 类看起来像这样

运行您的项目,如果一切正常,您将获得索引值。 PageIndex是获取当前索引的整数值。希望它会帮助你。

谢谢

曼迪普·辛格

于 2016-11-02T17:52:32.273 回答
4

实现委托方法

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
        NSLog(@"Current Page = %@", pageViewController.viewControllers);

        UIViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];

             if ([currentView isKindOfClass:[FirstPageViewController class]]) {
                 NSLog(@"First View");
            }
            else if([currentView isKindOfClass:[SecondPageViewController class]]){
                 NSLog(@"Second View");
            }
            else if([currentView isKindOfClass:[ThirdViewController class]]){
                  NSLog(@"Third View");
            }
 }

//pageViewController.viewControllers 总是返回当前可见的 View ViewController

于 2014-04-01T11:34:30.473 回答
2

要获取当前索引,您必须执行以下操作:

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {


   NSUInteger index = [self.viewControllers indexOfObject: [pageViewController.viewControllers lastObject]];

   NSLog(@"INDEX: %lu",(unsigned long)index);

}
于 2015-10-06T04:31:48.033 回答
2

我也为此苦苦挣扎,其他解决方案都不适合我,但最终能够使用委托方法解决它:

首先在 PageContentViewController.h 添加如下属性:

@property NSUInteger pageIndex;

其次在 PageContentViewController(不是 PageViewController)中创建一个委托协议。

在 PageContentViewController.h 中:

 @protocol PageContentDelegate <NSObject>

 - (void)sendBackPageIndex:(NSUInteger)index;

@end

@interface LKFPageContentViewController : (UIViewController)

@property (weak, nonatomic) id<LKFPageContentDelegate> delegate;
....
@end

PageContentViewController.m 中的第三个在 viewDidAppear 中执行委托方法

//Note that this must be done in viewDidAppear not viewDidLoad
- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:YES];

[self.delegate sendBackPageIndex:self.pageIndex];

、在PageViewController.m中实现委托方法

#pragma mark - PageContentView delegate methods

- (void)sendBackPageIndex:(NSUInteger)index {

    //This is where you get your current page index
    NSUInteger currentPageIndex = index;
}

第五,在 PageViewController.m 中确保传递 pageIndex 值并设置委托

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index {

    if (([self.arrayOfTitles count] == 0) || (index >= [self.arrayOfTitles count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];

    pageContentViewController.pageIndex = index;

    pageContentViewController.delegate = self;

    return pageContentViewController
}
于 2015-12-11T23:09:36.093 回答
2

我已经解决了这个问题:

* 父视图控制器 (PageViewController) *

@interface MyParentPageViewController () <UIPageViewControllerDataSource>


@property (nonatomic, assign) NSUInteger currentPageIndex;

@end

@implementation MyParentPageViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];

  MyChildViewController *rootChildViewController = [MyChildViewController controllerWithPageIndex:self.currentPageIndex];
  [self setViewControllers:@[rootChildViewController]
             direction:UIPageViewControllerNavigationDirectionForward
              animated:YES completion:nil];
  self.dataSource = self;
}

#pragma mark - Page View Controller DataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
viewControllerAfterViewController:(MyChildViewController *)viewController
{
  if (viewController.pageIndex == self.sourceArray.count - 1) {
    return nil;
  }
  MyChildViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex + 1];
  return vc;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(CHHomeDetailTableController *)viewController 
{
  if (viewController.pageIndex == 0) {
    return nil;
  }
  MyChilViewController *vc = [MyChildViewController controllerWithPageIndex:viewController.pageIndex - 1];
  return vc;
}


@end

* 子视图控制器 *

@implementation MyChildViewController
- (void)viewWillAppear
{
    MyParentPageViewController *parentPageViewController = (MyParentPageViewController *)self.parentViewController;
    parentViewController.currentPageIndex = self.pageIndex;
}
@end

比如 parentPageViewController 的 currentPageIndex 是 10 ,或者其他一些值,当你向右滑动时, parentViewController.currentPageIndex 会是 9,如果是 0,什么都没有发生;如果向左滑动,则 parentPageViewController.currentPageIndex 将为 11,如果显示的 controller.view 是最后一个,也没有任何反应。

于 2016-01-19T06:11:23.033 回答
0

You can try this. This will give you current page.

    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    UIViewController * viewController = [previousViewControllers lastObject];

    NSUInteger previousPage = [(PageContentViewController *)viewController index];

    NSUInteger currentPage = previousPage+1;
}
于 2015-04-15T13:33:16.933 回答
0

这就是我如何确定当前页面索引用户是向左还是向右滑动,以及他是否在滑动中间改变了滑动方向手势。只需确保您的视图控制器中有一个 pageIndex 属性来存储索引:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        let currentPageIndex = (pendingViewControllers.last! as! ViewController).pageIndex!
    }
于 2016-01-05T05:51:24.217 回答
0

没有很好的方法可以做到这一点,但我认为最可靠的方法是观察UIPageViewControllerDelegate调用:

var index: Int
var proposedViewController: UIViewController?

// MARK: UIPageViewControllerDelegate

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    guard let proposedViewController = proposedViewController,
        let index = myViewControllers?.index(of: proposedViewController) else {
            return
    }
    self.index = index
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    guard pendingViewControllers.count == 1,
        let proposedViewController = pendingViewControllers.first else {
        return
    }
    self.proposedViewController = proposedViewController
}
于 2017-07-31T20:30:10.193 回答