我试图在代码中将UIPageViewController添加到我的RootViewController中。我按照删除了故事板文件 Xcode 5 without Storyboard and ARC 并添加“常规”ViewController完美地工作。我根据UIPageViewControllerDataSource
调整了方法此外,我尝试简化逻辑(而不是从启动PageView项目实现ModelController我添加showVCWithIndex:方法从vcArray中选择3个VC中的1个并将其写入带有1个VC的Mutable Array。希望你能告诉我我是否正确或错了。
的.m
#import "RootPageViewController.h"
#import "ViewController0.h"
#import "ViewController1.h"
#import "ViewController2.h"
@interface RootPageViewController ()
@property (strong, nonatomic) ViewController0 *vController0;
@property (strong, nonatomic) ViewController1 *vController1;
@property (strong, nonatomic) ViewController2 *vController2;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *vcArray;
@property (strong, nonatomic) NSMutableArray *viewController;
@end
@implementation RootPageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
_vController0 = [[ViewController0 alloc] init];
_vController1 = [[ViewController1 alloc] init];
_vController2 = [[ViewController2 alloc] init];
NSUInteger index = 0;
NSMutableArray *viewController = [NSMutableArray arrayWithObjects:[self showVCWithIndex:index], nil];
[self.pageViewController setViewControllers:viewController
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self.vcArray indexOfObject:viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self showVCWithIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self.vcArray indexOfObject:viewController];
if (index == 2 || index == NSNotFound) {
return nil;
}
index++;
return [self showVCWithIndex:index];
}
- (UIViewController *)showVCWithIndex: (NSUInteger)index
{
self.vcArray = [NSArray arrayWithObjects: _vController0, _vController1, _vController2, nil];
UIViewController *currentVC = [self.vcArray objectAtIndex:index];
return currentVC;
}
@end
修改 得到了解决方案(这段代码有效,但我不确定我是否100%正确)
答案 0 :(得分:0)
在UIPageViewController
中设置视图控制器时,它必须是只有一个视图控制器的数组。方法-setViewControllers:direction:animated:completion:
是如此误导。当我第一次创建页面视图控制器时,我遇到了同样的问题。
要允许您的网页浏览控制器显示其他视图控制器,请使用UIPageViewControllerDataSource
方法-pageViewController:viewControllerBeforeViewController:
和-pageViewController:viewControllerAfterViewController:
返回它们。
答案 1 :(得分:0)
当你打电话......
[self.pageViewController setViewControllers:[vcArray objectAtIndex:0]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
...您正在为分页视图控制器设置nil
视图控制器。
[vcArray objectAtIndex:0]
指向_vController0
,在那一刻,nil
(您还没有实例化它)。
在将控制器设置为分页之前,您必须使用[self.storyboard instatiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"]
从故事板中获取它,或者只是在使用Xib时调用alloc init。