在故事板中使用UIPageViewController

Rez*_*ian 6 iphone xcode storyboard ios5 uipageviewcontroller

我正在寻找使用UIPageViewControllerXcode的故事板.我能找到的唯一例子就是如何使用单独的xib文件实现本教程UIPageViewcontroller.

但是,在本教程中,将UIPageViewController在扩展的类中以编程方式实例化UIViewController.这使得转换为故事板如何解释UIPageViewcontroller(这是一个单独的实例)变得复杂.

任何有关如何UIPageViewController使用Xcode的故事板创建功能的帮助将非常感激.

更新:我设法通过使用默认的pagecontroller模板在Xcode中创建一个新项目来解决此问题.它使用故事板,很容易遵循.

Rez*_*ian 8

更新:我设法通过使用默认的pagecontroller模板在xcode中创建一个新项目来解决此问题.它使用故事板,很容易遵循.


小智 7

将Xcode项目设置为PageViewController是实现此目的的一种方法,但是如果您想在已有的故事板中包含PageViewController,也可以执行此操作.

如果将PageViewController场景拖放到故事板上并连接一个segue,则可以推送到该PageViewController.但是,故事板中似乎存在一些错误,可以正确设置PageViewController,例如,您无法连接委托和数据源.

一个简单的方法是简单地在init方法中连接委托/数据源:

- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

这将导致您的委托和数据源方法被正确调用,假设您当然希望数据源和委托是PageViewController.设置完成后,您需要确保在加载视图时有一个视图控制器.您可以使用viewDidLoad中的PageViewController类上的setViewControllers方法执行此操作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setViewControllers:@[sweetViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
        NSLog(@"Hooray I set my initial viewcontroller for my page view controller");
    }];
}
Run Code Online (Sandbox Code Playgroud)

创建PageViewController时,它将从您的sweetViewController开始,然后根据需要开始调用您的数据源并委托方法.