0

我一直在努力让 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问题,这让我更加困惑......

在此先感谢,汤姆

4

1 回答 1

1

一般提示:

不使用[PageContentViewController new],使用[[PageContentViewController alloc] initWithNibName:nil bundle:nil]。不使用[UILabel new],使用[[UILabel alloc] initWithFrame:CGZeroRect]

单步执行您的代码。检查应该是对象的东西nil。Objective-C 会默默地允许事物存在nil,即使你在它们上调用方法!

于 2012-08-28T14:31:01.737 回答