Как реализовать UIPageViewController с UIScrollView?

Я взял пример Photo Scroller с веб-сайта Apple и попытался реализовать свой собственный альбом, скопировав код. Теперь UIScrollView не видно. Как мне сделать так, чтобы он появился?

Единственное изменение кода, которое я сделал, было при создании файла UIPageViewController. В моем случае его открывает UIViewController, а не AppDelegate.

@implementation BasePhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nibBundleOrNil];
    if (self) {
        // kick things off by making the first page

        PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
        if (pageZero != nil)
        {
            // assign the first page to the pageViewController (our rootViewController)
            //UIPageViewController *pageViewController = (UIPageViewController *)    [[UIApplication sharedApplication] keyWindow].rootViewController;
            UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0 navigationOrientation:0 options:nil];
            //UIPageViewController *pageViewController = (UIPageViewController *)self.parentViewController;
            pageViewController.dataSource = self;

            [pageViewController setViewControllers:@[pageZero]
                                     direction:UIPageViewControllerNavigationDirectionForward
                                      animated:NO
                                    completion:NULL];
        }
    }
    return self;
}

person rahulg    schedule 18.06.2013    source источник


Ответы (1)


Вы не добавляете представление pageViewController в качестве подпредставления представления BasePhotoViewController. Ваш класс BasePhotoViewController должен выглядеть примерно так. Обратите внимание на код в viewDidLoad.

BasePhotoViewController.h:

@interface BasePhotoViewController : UIViewController <UIPageViewControllerDataSource>
@property (nonatomic, strong) UIPageViewController * pageViewController;
@end

BasePhotoViewController.m:

#import "BasePhotoViewController.h"
#import "PhotoViewController.h"

@implementation BasePhotoViewController

@synthesize pageViewController;

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
        if (pageZero != nil)
        {
            self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0
                                                                      navigationOrientation:0
                                                                                    options:nil];
            self.pageViewController.dataSource = self;

            [self.pageViewController setViewControllers:@[pageZero]
                                              direction:UIPageViewControllerNavigationDirectionForward
                                               animated:NO
                                             completion:NULL];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
}

# pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(PhotoViewController *)vc
{
    NSUInteger index = vc.pageIndex;
    return [PhotoViewController photoViewControllerForPageIndex:(index - 1)];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)vc
{
    NSUInteger index = vc.pageIndex;
    return [PhotoViewController photoViewControllerForPageIndex:(index + 1)];
}

@end

Примечание. Я инициализировал UIPageViewController в initWithCoder:, потому что Photo Scroller использует раскадровку. Я удалил UIPageViewController из раскадровки и создал на его месте BasePhotoViewController. Если вы не загружаете BasePhotoViewController из раскадровки, вы должны переместить код в initWithCoder: в соответствующий инициализатор.

EDIT: См. этот пример проекта на github.

person Steph Sharp    schedule 20.06.2013