UIPageViewController:获取currentPage

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIPageViewController:获取currentPage相关的知识,希望对你有一定的参考价值。

在过去的几天里,我一直在努力解决这个问题,毕竟这个杂耍我已经发现我需要的只是数据源方法的当前索引,用当前可见的页码更新

我有这个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;
}

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

- (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;
}

我怎么能纠正这个?

答案

看起来您应该能够获得当前索引:

ContentViewController *viewController = [self.pageViewController.viewControllers lastObject];
currentIndex = [modelArray indexOfObject:[viewController page]];
另一答案

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

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        let currentPageIndex = (pendingViewControllers.last! as! ViewController).pageIndex!
    }
另一答案

没有很好的方法可以做到这一点,但我认为最可靠的方法是观察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
}
另一答案

我的方法很简单:

  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
    }

}

摘要点是在数据源中创建页面时设置页面索引。使用这种方法,以后可以非常直接地检索它。

另一答案

对于swift:

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

}
另一答案

如果您实现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];
}
另一答案

实现委托方法

- (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

另一答案

我在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数据的类。 My PageContentViewController class looks like this

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

谢谢

曼迪普辛格

另一答案

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

-(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);

}
另一答案

我也在努力解决这个问题,其他解决方案都没有为我工作,但最终能够使用委托方法解决它:

首先在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
}
另一答案

我已经解决了这个问题:

*父视图控制器(PageViewController)*

@interface MyParentPageViewController () <UIPageViewControllerDataSource>


@property (nonatomic, assign) NSUInteger currentPageIndex;

@end

@implementation MyParentPageViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];

  MyChil

以上是关于UIPageViewController:获取currentPage的主要内容,如果未能解决你的问题,请参考以下文章

在 UIPageViewController 中获取用户滑动方向

如何在 UIPageViewController 中获取当前控制器

我能以某种方式从 UIPageViewController 获取 scrollViewDidScroll: UIScrollView Delegate 方法吗?

减慢 UIPageViewController 分页

在几秒钟内重新加载 UiPageViewController

如何用 UIScrollView 实现 UIPageViewController?