UIPageViewController: get the currentPage

2019-03-14 17:25发布

I have been struggling with this issue for the last few days and after all this juggling I have figured out that all I need is the current Index from the datasource method to update with current visible page number

I have this UIPageViewController datasource method and I need to use the current index to get the current visible page for delegate method 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;
}

I'm confused about how to write the current index statement from datasource method into delegate method to update current visible page.

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

How can I correct this?

12条回答
欢心
2楼-- · 2019-03-14 17:47

I struggled with this too and none of the other solutions worked for me, but was finally able to solve it using a delegate method:

First in PageContentViewController.h Add the following property:

@property NSUInteger pageIndex;

Second create a delegate protocol in the PageContentViewController (not the PageViewController).

In PageContentViewController.h:

 @protocol PageContentDelegate <NSObject>

 - (void)sendBackPageIndex:(NSUInteger)index;

@end

@interface LKFPageContentViewController : (UIViewController)

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

Third in PageContentViewController.m perform delegate method in viewDidAppear

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

[super viewDidAppear:YES];

[self.delegate sendBackPageIndex:self.pageIndex];

Fourth, in PageViewController.m implement the delegate method

#pragma mark - PageContentView delegate methods

- (void)sendBackPageIndex:(NSUInteger)index {

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

Fifth, in PageViewController.m make sure you pass the pageIndex value and set the delegate

- (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
}
查看更多
Summer. ? 凉城
3楼-- · 2019-03-14 17:48

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

ContentViewController *viewController = [self.pageViewController.viewControllers lastObject];
currentIndex = [modelArray indexOfObject:[viewController page]];
查看更多
做个烂人
4楼-- · 2019-03-14 17:49

There is no nice way to do this but I think the most reliable way is to observe UIPageViewControllerDelegate calls:

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
}
查看更多
男人必须洒脱
5楼-- · 2019-03-14 17:54

for swift:

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

}
查看更多
小情绪 Triste *
6楼-- · 2019-03-14 17:57

Implement Delegate Method

- (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 always return current visible View ViewController

查看更多
地球回转人心会变
7楼-- · 2019-03-14 17:57

I am facing the same issue on pageViewController, everytime i swipe my pageViewController it calls my delegate twice and am not able to find the issue. And my array is dynamic so i want the proper index value from pageController. First of all set the delegate of your pageViewController. Like this :-

self.pageViewController.delegate = self;

And then add this delegate method to your class

- (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 is the class where you are showing your data of pageViewController. My PageContentViewController class looks like this

Run your project and if everything is working fine, you will get your index value. PageIndex is the Integer value for getting the current index. Hope it will helps you.

Thanks

Mandeep Singh

查看更多
登录 后发表回答