访问页面控制器中的多个视图控制器 [英] Accessing multiple view controllers in page controller

查看:78
本文介绍了访问页面控制器中的多个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ipad中显示视图就像一本书,单个视图显示两个视图。我想添加更多视图,以便在视图翻转时显示第三和第四视图并进一步显示。我使用下面的代码来做到这一点。我将ViewControllers添加到数组,它在此行的方向方法中获取了killContentViewController * currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];。

I am showing view in ipad like a book, single view shows two view. I want to add more views so that when view flipped third and fourth view appears and further. I am using the code below to do so. I am adding ViewControllers to array it got kill at orientation method at this line " 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;
}
}


推荐答案

你无法在开头使用您想要的所有视图控制器设置 UIPageViewController viewControllers 属性。

You cannot set the viewControllers property of the UIPageViewController with all the view controllers you want in the beginning.

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

You must implement the following methods for the data source: - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController and - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController. This provides the view controllers to show when changing page. You must return nil when there are no more pages to show.

所以,当你在 viewDidLoad 时,你设置 viewControllers 包含3个元素的属性实际上应该只用两个元素设置它们,因为你只允许一次显示两个页面。

So, when on viewDidLoad you set the viewControllers property with 3 elements you actually should set them with only two as you are only allowed to show two pages at once.

这篇关于访问页面控制器中的多个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆