我一直在努力让UIPageViewController工作很长一段时间,而且缺乏结果开始让我疯狂......
以下代码是部分教程(我也没有开始工作)和部分我。现在我正在尝试让每个viewControllers查看一个标签,这样我就可以确保它在添加内容之前有效。
- (void)viewDidLoad
{
[super viewDidLoad];
vcArray = [self createViewControllers:1];
NSLog(@"TEST1");
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.delegate = self;
self.pageController.dataSource = self;
NSLog(@"TEST2 %@", vcArray);
[self.pageController setViewControllers:vcArray direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[[pageController view] setFrame:[[self view] bounds]];
NSLog(@"TEST3");
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[pageController didMoveToParentViewController:self];
NSLog(@"TEST4");
}
- (NSArray *) createViewControllers:(NSInteger)ammount{
NSMutableArray *array = [NSMutableArray new];
for (NSInteger i = 0; i <= ammount; i++) {
PageContentViewController *pageContentViewController = [PageContentViewController new];
UILabel *label = [UILabel new];
label.text = @"STRING";
pageContentViewController.view = label;
[array addObject:pageContentViewController];
}
return array;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(PageContentViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [vcArray objectAtIndex:index];
}
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(PageContentViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [vcArray count]) {
return nil;
}
return [vcArray objectAtIndex:index];
}
- (NSUInteger)indexOfViewController:(PageContentViewController *)viewController
{
return [vcArray indexOfObject:viewController.dataObject];
}
你能看到我错过的东西吗? 我一直在经历太多的教程和stackoverflow问题,这让我更加困惑......
提前致谢, 汤姆
答案 0 :(得分:1)
一般提示:
请勿使用[PageContentViewController new]
,请使用[[PageContentViewController alloc] initWithNibName:nil bundle:nil]
。请勿使用[UILabel new]
,请使用[[UILabel alloc] initWithFrame:CGZeroRect]
。
逐步完成代码。检查应该是nil
对象的内容。 Objective-C会默默地允许事物nil
,即使你在它们上面调用方法!