2012-11-16 208 views
0

我在ipad中显示的视图就像一本书,单个视图显示两个视图。我想添加更多视图,以便在查看翻转的第三个和第四个视图出现并进一步显示时。我正在使用下面的代码来这样做。我将视图控制器添加到数组它在定位方法在这一行杀死“ContentViewController * currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];”。在页面控制器中访问多个视图控制器

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

//Instantiate the model array 
self.modelArray = [[NSMutableArray alloc] init]; 
for (int index = 1; index <= 12 ; index++) 
{ 
    [self.modelArray addObject:[NSString stringWithFormat:@"Page %d",index]]; 
} 

//Step 1 
//Instantiate the UIPageViewController. 
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl 
                  navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 

//Step 2: 
//Assign the delegate and datasource as self. 
self.pageViewController.delegate = self; 
self.pageViewController.dataSource = self; 

//Step 3: 
//Set the initial view controllers. 

ViewOne *one = [[ViewOne alloc]initWithNibName:@"ViewOne" bundle:nil]; 
viewTwo *two = [[viewTwo alloc]initWithNibName:@"ViewTwo" bundle:nil]; 

ContentViewController *contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil]; 
contentViewController.labelContents = [self.modelArray objectAtIndex:0]; 

// NSArray *viewControllers = [NSArray arrayWithObject:contentViewController]; 
viewControllers = [NSArray arrayWithObjects:contentViewController,one,two,nil]; 

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



//Step 4: 
//ViewController containment steps 
//Add the pageViewController as the childViewController 
[self addChildViewController:self.pageViewController]; 

//Add the view of the pageViewController to the current view 
[self.view addSubview:self.pageViewController.view]; 

//Call didMoveToParentViewController: of the childViewController, the UIPageViewController instance in our case. 
[self.pageViewController didMoveToParentViewController:self];  

//Step 5: 
// set the pageViewController's frame as an inset rect. 
CGRect pageViewRect = self.view.bounds; 
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0); 
self.pageViewController.view.frame = pageViewRect; 

//Step 6: 
//Assign the gestureRecognizers property of our pageViewController to our view's gestureRecognizers property. 
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; 

} 

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController 
       spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
if(UIInterfaceOrientationIsPortrait(orientation)) 
{ 
    //Set the array with only 1 view controller 
    UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 
    NSArray *viewControllers = [NSArray arrayWithObject:currentViewController]; 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 

    //Important- Set the doubleSided property to NO. 
    self.pageViewController.doubleSided = NO; 
    //Return the spine location 
    return UIPageViewControllerSpineLocationMin; 
} 
else 
{ 


    // NSArray *viewControllers = nil; 

    ContentViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 

    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)currentViewController labelContents]]; 
    if(currentIndex == 0 || currentIndex %2 == 0) 
    { 
     UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController]; 
     viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil]; 
    } 
    else 
    { 
     UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController]; 
     viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil]; 
    } 
    //Now, set the viewControllers property of UIPageViewController 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 

    return UIPageViewControllerSpineLocationMid; 
} 
} 

回答

0

你不能和你在一开始要的所有视图控制器设置UIPageViewControllerviewControllers财产。

您必须为数据源实现以下方法:- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController。这提供了视图控制器在更改页面时显示。当没有更多页面显示时,您必须返回零。

因此,如果您在viewDidLoad上设置了viewControllers属性和3个元素,您实际上应该只设置它们两个,因为您只允许一次显示两个页面。

+0

我在initWithViewControllers方法初始化它从委托调用后,它进入viewDidLoad,所以当我尝试通过调用上面定义的方法移动到下一个视图控制器数组不会初始化。请检查此链接“http://stackoverflow.com/questions/13875151/array-value-not-retrieving-outside-method-initwithviewcontrollers” –

相关问题