UIPageViewController: get the currentPage

后端 未结 12 526
别那么骄傲
别那么骄傲 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:49

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

    I have solved this problem like this:

    * Parent View Controller (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
    

    * Child View Controller *

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

    For example, the parentPageViewController's currentPageIndex is 10 , or some other values, when your swipe right, parentViewController.currentPageIndex will be 9, if 0 , nothing happened; If you swipe left, parentPageViewController.currentPageIndex will be 11, if the showing controller.view is the last, nothing happened too.

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

    This is how I determine the current page index whether the user swipes to the left or to the right and whether he changes his swipe direction gesture in the middle of swiping. Just make sure you have a pageIndex property in your view controller to store the index:

    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
            let currentPageIndex = (pendingViewControllers.last! as! ViewController).pageIndex!
        }
    
    0 讨论(0)
  • 2021-02-03 22:54

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

    ContentViewController *viewController = [self.pageViewController.viewControllers lastObject];
    currentIndex = [modelArray indexOfObject:[viewController page]];
    
    0 讨论(0)
  • 2021-02-03 22:55

    if you implement ModelController (as in Apple example), use it's method 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];
    }
    
    0 讨论(0)
  • 2021-02-03 22:58

    To get the current index you'll have to do the following:

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