UIPageviewcontroller:从中间开始

时间:2015-01-05 17:08:22

标签: objective-c uipageviewcontroller

不幸的是搜索没有帮助我......我有以下问题:我有三个UItableviews(ViewController1,ViewController2和Viewcontroller3),它嵌入了一个简单的PageViewController Screenshot of storyboard(我无法插入这里有任何图片:/)。

为了显示三个ViewControllers,我将它们放在我的PageViewController.m(PageViewController的自定义类)的viewdidload中的数组中,所以我没有使用ViewController作为容器来将其他ViewControllers放入:

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
    self.dataSource = self;
    
    UIViewController *p1 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView1"];
    UIViewController *p2 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView2"];
    UIViewController *p3 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView3"];
    
    myViewControllers = @[p1,p2,p3];
    
我可以选择我开始的哪个viewcontroller和

- 前进(方向:UIPageViewControllerNavigation**DirectionForward**):

        [self setViewControllers:@[p1]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO completion:nil];
        
        NSLog(@"loaded!");
    }

或选择最后一个并向后搜索UIPageViewControllerNavigation**DirectionReverse**):

        [self setViewControllers:@[p3]
                   direction:UIPageViewControllerNavigationDirectionReverse
                   animated:NO completion:nil];
      
 

但是,我一直在试图弄清楚如何从ViewController2开始,左侧有Viewcontroller1,右侧ViewController3#import "StartScreenPageViewController.h" @interface StartScreenPageViewController () @end @implementation StartScreenPageViewController { NSArray *myViewControllers; } -(void)viewDidLoad { [super viewDidLoad]; self.delegate = self; self.dataSource = self; UIViewController *p1 = [self.storyboard instantiateViewControllerWithIdentifier:@"1"]; UIViewController *p2 = [self.storyboard instantiateViewControllerWithIdentifier:@"2"]; UIViewController *p3 = [self.storyboard instantiateViewControllerWithIdentifier:@"3"]; myViewControllers = @[p1,p2,p3]; [self setViewControllers:@[p1] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; NSLog(@"loaded!"); } -(UIViewController *)viewControllerAtIndex:(NSUInteger)index { return myViewControllers[index]; } -(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSUInteger currentIndex = [myViewControllers indexOfObject:viewController]; --currentIndex; currentIndex = currentIndex % (myViewControllers.count); return [myViewControllers objectAtIndex:currentIndex]; } -(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSUInteger currentIndex = [myViewControllers indexOfObject:viewController]; ++currentIndex; currentIndex = currentIndex % (myViewControllers.count); return [myViewControllers objectAtIndex:currentIndex]; } -(NSInteger)presentationCountForPageViewController: (UIPageViewController *)pageViewController { return myViewControllers.count; } -(NSInteger)presentationIndexForPageViewController: (UIPageViewController *)pageViewController { return 0; } @end

非常感谢任何建议或代码段!

这是整个代码:

{{1}}

1 个答案:

答案 0 :(得分:1)

您应该能够在viewDidLoad中设置初始视图控制器。您已经在执行此操作,但是您已将其设置为第一个视图控制器:

 [self setViewControllers:@[p1]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];

我认为您应该能够将其更改为:

 [self setViewControllers:@[p2]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];

然后确保您的pageViewController:viewControllerAfterViewControllerpageViewController:viewControllerBeforeViewController方法可以根据您所需的功能处理数据源的上下迭代。