如何将多个 ViewController 添加到我的 UIPageViewController? [英] How do I add multiple ViewControllers to my UIPageViewController?

查看:61
本文介绍了如何将多个 ViewController 添加到我的 UIPageViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 Obj-C 还很陌生,并尝试查看示例代码和其他在线资源来回答我的问题,但我似乎找不到任何真正有帮助的东西.基本上我想要做的是将我在 Storyboard 中创建的多个 UIViewController 添加到 UIPageViewController - 我寻求帮助的第一个地方是使用 PageBased 应用程序模板的 Xcode 本身.这实际上有很大帮助,我启动并运行了一个 PageController.然后我转向在线资源,但大多数(如果不是全部)使用单个 viewController(例如 这个).然后我转向 StackOverflow 并发现以下内容 - 如何实现利用 UIPageViewController多个视图控制器.以上资源让我走到了这一步:在 PageViewController.h 中:

so I'm pretty new to Obj-C and have tried looking at sample code and other online resources to answer my question, but I can't seem to find anything that really helps. Essentially what I'm trying to do is to add multiple UIViewControllers that I created in Storyboard to a UIPageViewController - the first place I looked to for help was Xcode itself by using a template of a PageBased application. That actually helped quite a lot and I got a PageController up and running. Then I turned to online resources but most (if not all of them) use a single viewController (like this). Then I turned to StackOverflow and found the following - How to implement UIPageViewController that utilizes multiple ViewControllers. The above resources got me this far: In PageViewController.h:

    #import <UIKit/UIKit.h>

@interface PageViewController : UIPageViewController <UIPageViewControllerDataSource>

@end

在 PageViewController.m 中:

In PageViewController.m:

    #import "PageViewController.h"

@interface PageViewController ()

@end

@implementation PageViewController {
    NSArray *viewControllers;
    UIViewController *first;
    UIViewController *second;
    UIViewController *third;
    UIViewController *fourth;
    UIViewController *fifth;
}

- (UIViewController *)first {

        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        first = [sb instantiateViewControllerWithIdentifier:@"1"];

    return first;
}

- (UIViewController *)second {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    second = [sb instantiateViewControllerWithIdentifier:@"2"];

    return second;
}
- (UIViewController *)third {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    third = [sb instantiateViewControllerWithIdentifier:@"3"];

    return third;
}
- (UIViewController *)fourth {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    fourth = [sb instantiateViewControllerWithIdentifier:@"4"];

    return fourth;
}
- (UIViewController *)fifth {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    fifth = [sb instantiateViewControllerWithIdentifier:@"5"];

    return fifth;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;

    // Aggancio il view controller iniziale.
    [self setViewControllers:@[self.first]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    UIViewController *nextViewController = nil;

    if (viewController == self.first) {
        nextViewController = self.second;
    }
    if (viewController == self.second) {
        nextViewController = self.third;
    }
    if (viewController == self.third) {
        nextViewController = self.fourth;
    }
    if (viewController == fourth) {
        nextViewController = self.fifth;
    }

    return nextViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    UIViewController *prevViewController = nil;

    if (viewController == self.fifth) {
        prevViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        prevViewController = self.third;
    }
    if (viewController == self.third) {
        prevViewController = self.second;
    }
    if (viewController == self.second) {
        prevViewController = self.first;
    }

    return prevViewController;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

所有这些代码所做的就是加载第一个视图控制器,它现在可以滑动,但实际上并没有向任何东西滑动 - 为什么会这样?.我究竟做错了什么?上述资源使用标题和页面来创建视图,我真的不知道该怎么做.有人会介意引导我或将我推向正确的方向吗?任何帮助将不胜感激.谢谢!

All this code does is load the first view controller which is now swipe-able, but doesn't actually swipe towards anything - why is this?. What am I doing wrong? The aforementioned resources used titles and pages to create views and I really don't know how to go about this. Would anyone mind guiding me or nudging me in the correct direction? Any help would be greatly appreciated. Thanks!

推荐答案

如果有人感兴趣,这里是完整的代码.我花了一段时间才把它弄对,但最终这工作得很好!

Here is the fully working code if anyone is interested. Took me a while to get it right, but eventually this worked perfectly!

#import "PageViewController.h"

@interface PageViewController ()

@property (nonatomic, retain) UIViewController *first;
@property (nonatomic, retain) UIViewController *second;
@property (nonatomic, retain) UIViewController *third;
@property (nonatomic, retain) UIViewController *fourth;
@property (nonatomic, retain) UIViewController *fifth;

@end

@implementation PageViewController {
    NSArray *viewControllers;
}

- (UIViewController *)first {
    if (!_first) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        _first = [sb instantiateViewControllerWithIdentifier:@"1"];
    }
    return _first;
}

- (UIViewController *)second {
    if (!_second) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _second = [sb instantiateViewControllerWithIdentifier:@"2"];
    }
    return _second;
}
- (UIViewController *)third {
    if (!_third) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _third = [sb instantiateViewControllerWithIdentifier:@"3"];
    }
    return _third;
}
- (UIViewController *)fourth {
    if (!_fourth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fourth = [sb instantiateViewControllerWithIdentifier:@"4"];
    }
    return _fourth;
}
- (UIViewController *)fifth {
    if (!_fifth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fifth = [sb instantiateViewControllerWithIdentifier:@"5"];
    }
    return _fifth;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;

    // Aggancio il view controller iniziale.
    [self setViewControllers:@[self.first]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    UIViewController *nextViewController = nil;

    if (viewController == self.first) {
        nextViewController = self.second;
    }
    if (viewController == self.second) {
        nextViewController = self.third;
    }
    if (viewController == self.third) {
        nextViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        nextViewController = self.fifth;
    }

    return nextViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    UIViewController *prevViewController = nil;

    if (viewController == self.fifth) {
        prevViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        prevViewController = self.third;
    }
    if (viewController == self.third) {
        prevViewController = self.second;
    }
    if (viewController == self.second) {
        prevViewController = self.first;
    }

    return prevViewController;
}

@end

这篇关于如何将多个 ViewController 添加到我的 UIPageViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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