2013-07-12 61 views
1

我無法找到ViewView控制器作爲頁面的PageViewController的工作示例。PageViewController with ViewControllers

我只需要兩個水平視圖控制器:viewcontroller1和viewcontroller2。

這裏是我的viewDidLoad:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil]; 

    self.pageController.dataSource = self; 
    [[self.pageController view] setFrame:[[self view] bounds]]; 

    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 
    controller1 = [storyboard instantiateViewControllerWithIdentifier:@"first"]; 
    controller2 = [storyboard instantiateViewControllerWithIdentifier:@"second"]; 

    viewControllers = [NSArray arrayWithObjects:controller1, nil]; 

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

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


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 
    return controller1; 
} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 
    return controller2; 
} 

的問題是,刷卡第二種觀點之後,就消失了。 一些幫助理解PageViewController會很好。

在此先感謝

回答

5

更改您委派這樣的方法,它應該正常工作:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 
    if (self.pageController.viewControllers[0] == self.controller2) 
     return self.controller1; 
    return nil; 
} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 
    if (self.pageController.viewControllers[0] == self.controller1) 
     return self.controller2; 
    return nil; 
}