2013-06-18 60 views
1

我拿了蘋果網站上的Photo Scroller的例子,並試圖通過複製代碼來實現我自己的專輯。現在,UIScrollView不可見。我如何讓它出現?如何用UIScrollView實現UIPageViewController?

我唯一的代碼更改是在創建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; 
} 

回答

3

您沒有添加pageViewController的觀點爲BasePhotoViewController的子視圖的視圖。你的BasePhotoViewController類應該看起來像這樣。請注意0​​中的代碼。

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 

注:因爲蘋果的Photo Scroller代碼使用了一個故事板,我在初始化的initWithCoder:UIPageViewController。我從故事板中刪除了UIPageViewController,並在其位置創建了一個BasePhotoViewController。如果您未從故事板加載BasePhotoViewController,則應將initWithCoder:中的代碼移至相應的初始程序。

編輯:在github上看到這個sample project

相關問題