In UIPageViewController, when “pageViewController:viewControllerBeforeViewController”get called?

二次信任 提交于 2019-12-12 12:14:06

问题


I try to display photos in UIPageViewController. Firstly I display my photos in a collection view and then tap one photo to enter page view. When I'm in page view, however, if I scroll the currently photo(to right or left), I find that both pageViewController:viewControllerBeforeViewController and pageViewController:viewControllerAfterViewController method will be called(by calling NSLog I found it).

Should just one of the two methods be called once? I am quite confused by this and have big trouble updating my photoNumber for displaying the photo I want.

Here is my page view code:

#import "PhotoPageViewController.h"
#import "PhotoViewController.h"

@interface PhotoPageViewController ()
@property (nonatomic)NSInteger previousPhotoNumber;
@end

@implementation PhotoPageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataSource = self;
    self.delegate = self;

    self.numberOfPhotosInCurrentAlbum = [Photo numberOfPhotosInAlbumWithName:self.albumName inManagedObjectContext:self.context];
    PhotoViewController *currentPhotoViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PhotoContentViewController"];
    currentPhotoViewController.photoNumber = self.currentPhotoNumber;
    currentPhotoViewController.albumName = self.albumName;
    currentPhotoViewController.context = self.context;

    currentPhotoViewController.ppvc = self;
    currentPhotoViewController.numberOfPhotosInCurrentAlbum =self.numberOfPhotosInCurrentAlbum;

    [self setViewControllers:@[currentPhotoViewController]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO
                  completion:NULL];
}



#pragma mark - Page View Controller Datasource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (self.currentPhotoNumber > 0) {
        NSLog(@"in before section: current photo %lu", self.currentPhotoNumber);

        PhotoViewController *photoBeforeCurrentPhoto = [self.storyboard instantiateViewControllerWithIdentifier:@"PhotoContentViewController"];
        photoBeforeCurrentPhoto.photoNumber = self.currentPhotoNumber - 1;
        photoBeforeCurrentPhoto.albumName = self.albumName;
        photoBeforeCurrentPhoto.context = self.context;
        photoBeforeCurrentPhoto.ppvc = self;

        self.previousPhotoNumber = self.currentPhotoNumber;
        self.currentPhotoNumber--;

        return photoBeforeCurrentPhoto;
    }
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (self.currentPhotoNumber < self.numberOfPhotosInCurrentAlbum - 1) {
        NSLog(@"in after section: current photo %lu", self.currentPhotoNumber);
        PhotoViewController *photoAfterCurrentPhoto = [self.storyboard instantiateViewControllerWithIdentifier:@"PhotoContentViewController"];
        photoAfterCurrentPhoto.photoNumber = self.currentPhotoNumber + 1;
        photoAfterCurrentPhoto.albumName = self.albumName;
        photoAfterCurrentPhoto.context = self.context;
        photoAfterCurrentPhoto.ppvc = self;

        self.previousPhotoNumber = self.currentPhotoNumber;
        self.currentPhotoNumber++;

        return photoAfterCurrentPhoto;
    }
    return nil;
}

PhotoViewController is the view controller to display photo.

Can anyone give me some advice about the usage of UIPageViewController?

Thanks in advance!


回答1:


UIPageViewController uses these bellow method for getting the back and front ViewController.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

Though its weird but yes its a known issue. Every time it calls Before and After method to get VC. It there is no next VC then it returns nil and if there is no previous VC the datasourceDelegate return nil, otherwise it return the index of VC.

In UIPageViewControllerDelegate, there is a function named :

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;

Look at this it might help to get the current or next/previous viewcontroller from pendingViewControllers array.

Hope this helps.. :)



来源:https://stackoverflow.com/questions/22978879/in-uipageviewcontroller-when-pageviewcontrollerviewcontrollerbeforeviewcontro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!