UIPageViewController: get the currentPage

后端 未结 12 525
别那么骄傲
别那么骄傲 2021-02-03 22:22

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 up

相关标签:
12条回答
  • 2021-02-03 22:38

    My approach is quite simple:

    1. When the page (controller) is instantiated I pass in the page index, which at this point is known.

    2. Then in the delegate call below, iff the transition finished, I obtain the controller currently shown and cast it to my custom page class, before I fetch and update the currentPage with the associated page index.

    To keep things simple I setup the data source and delegate to be the WalkThroughController which embeds the page view controller.

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

    The take away point is to set the page index of the pages when they are created in the data source. With this approach it is very straight forward to retrieve it later.

    0 讨论(0)
  • 2021-02-03 22:44

    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.

    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

    0 讨论(0)
  • 2021-02-03 22:45

    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
    }
    
    0 讨论(0)
  • 2021-02-03 22:46

    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

    0 讨论(0)
  • 2021-02-03 22:47

    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;
    }
    
    0 讨论(0)
  • 2021-02-03 22:49

    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
    
    }
    
    0 讨论(0)
提交回复
热议问题