Ios5 如何在iOS 5中向UIPageViewController添加页面数组?

Ios5 如何在iOS 5中向UIPageViewController添加页面数组?,ios5,nsarray,uipageviewcontroller,uistoryboard,Ios5,Nsarray,Uipageviewcontroller,Uistoryboard,有人知道我在将自己的视图控制器手动添加到UIPageViewController方法时缺少了什么吗 我目前有这个,我不知道如何继续: NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin

有人知道我在将自己的视图控制器手动添加到UIPageViewController方法时缺少了什么吗

我目前有这个,我不知道如何继续:

NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin], nil];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:pageViewOptions];


BookViewController *pageOne = [self.storyboard instantiateViewControllerWithIdentifier:@"BookViewController"];

AnotherPage *pageTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherPage"];

PageThree *pageThree = [self.storyboard instantiateViewControllerWithIdentifier:@"PageThree"];


self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

[self.pageViewController setViewControllers:[NSArray arrayWithObjects:pageOne, pageTwo, pageThree, nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];

[self.view addSubview:self.pageViewController.view];

self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

[self.pageViewController didMoveToParentViewController:self];
但它似乎不起作用。我只看到RootViewController


提前感谢你们所有喜欢帮助像我这样热心的新手的人……)

我发现这里有一些问题。我自己也有点不在行,所以我不知道这是否能修复代码中的所有内容

  • 您需要一个UIPageViewController,然后在需要时实例化各个页面的视图。传递视图数组效率不高(占用太多内存)

  • 您需要在包含UIPageViewController子类的类中实现UIPageViewControllerDataSource协议。代码段中的大部分(但不是全部)代码都将放在这里。有关说明,请参阅or

  • 使用手势识别器,您可能需要做更多的工作。如果使用IBOutlet,则可以通过界面生成器进行管道安装

  • 希望这有帮助。

    试试这个功能:

    setViewControllers:direction:animated:completion:
    
    设置要显示的视图控制器

    - (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion
    
    参数

    视图控制器:要显示的一个或多个视图控制器

    - (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion
    
    方向:导航方向

    已设置动画:一个布尔值,指示是否要设置过渡动画

    完成:翻页动画完成时调用的块

    该块采用以下参数:

    完成
    如果动画完成,则为“是”;如果跳过,则不会。设置此项的方式与
    UIPageViewController
    的工作方式不太一样

    首先要了解的是,
    setViewControllers:direction:animated:completion
    仅设置可见视图控制器,而不是您希望向用户显示的所有视图控制器。(也就是说,如果我试图在代码中设置一个由三个控制器组成的数组,我会得到一个异常。)

    其次,如果有多个可见页面(或两个,取决于脊椎位置),则必须实现
    UIPageViewControllerDataSource
    协议,以提供页面视图控制器将显示的视图控制器

    下面是一个简短的示例(作为
    UIPageViewController
    的子类实现,但也可以与子页面视图控制器一起使用…只需将
    self.dataSource=…
    替换为
    myPageViewController.dataSource=…

    @实现MyPageViewController{
    //我们将页面视图控制器保存在一个数组中,但我们也可以轻松地
    //在数据源方法中动态实例化它们
    NSArray*_页;
    }
    -(作废)第页{
    /*
    *设置三个页面,每个页面的背景颜色不同
    */
    UIViewController*a=[[UIViewController alloc]initWithNibName:nil bundle:nil];
    a、 view.backgroundColor=[UIColor redColor];
    UIViewController*b=[[UIViewController alloc]initWithNibName:nil bundle:nil];
    b、 view.backgroundColor=[UIColor greenColor];
    UIViewController*c=[[UIViewController alloc]initWithNibName:nil bundle:nil];
    c、 view.backgroundColor=[UIColor blueColor];
    _页码=@[a、b、c];
    }
    -(无效)viewDidLoad{
    [超级视图下载];
    [自我设置页面];
    self.dataSource=self;
    //设置初始可见页面的视图控制器…如果不执行此操作
    //你什么也看不到。
    [self-setViewControllers:@[_pages[0]]方向:UIPageViewControllerNavigationDirectionForward动画:是完成:^(布尔完成){
    }];
    }
    #pragma标记-UIPageViewControllerDataSource
    -(UIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerAfterViewController:(UIViewController*)viewController{
    if(nil==viewController){
    返回_页[0];
    }
    NSInteger idx=[\u页面索引对象:viewController];
    NSParameterAssert(idx!=NSNotFound);
    如果(idx>=[\u页数]-1){
    //我们在_pages数组的末尾
    返回零;
    }
    //返回下一页的视图控制器
    返回_页[idx+1];
    }
    -(UIViewController*)页面视图控制器:(UIPageViewController*)页面视图控制器视图控制器预览控制器:(UIViewController*)视图控制器{
    if(nil==viewController){
    返回_页[0];
    }
    NSInteger idx=[\u页面索引对象:viewController];
    NSParameterAssert(idx!=NSNotFound);
    
    如果(idx)你有没有发现这个问题?我也遇到了同样的问题