0

我正在尝试实现一个UIPageViewController. 我创建了 4 个单独的 viewController 并将其添加到我的故事板中。现在我想用UIPageViewController. 这就是我在代码中所做的。

我做了一个modelArray,把所有的VC都放进去:

   self.modelArray = [[NSMutableArray alloc] init];
   NSMutableArray *pageData = [[NSMutableArray alloc]init];
    ViewOneViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"];
    ViewTwoViewController *viewTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewTwo"];
    ViewThreeViewController *viewThree = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewThree"];
    ViewFourViewController *viewFour = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewFour"];

    [pageData addObject:viewOne];
    [pageData addObject:viewTwo];
    [pageData addObject:viewThree];
    [pageData addObject:viewFour];

    self.modelArray = pageData;

接下来设置 VC 的基础知识UIPageViewController并设置 Initial VC :

 ViewOneViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"];
    NSArray *viewControllers = [NSArray arrayWithObject:cVC];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Add the pageViewController as the childViewController
    [self addChildViewController:self.pageViewController];

    // Add the view of pageViewController  to the currentView
    [self.view addSubview:self.pageViewController.view];

    // Call didMoveToParentViewController: of the childViewController, the UIPageViewController instance in our case.
    [self.pageViewController didMoveToParentViewController:self];

    // Assign the gestureRecognizers property of the pageViewController to the view's gestureRecognizers property
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

最后这些是我的delegate方法(例如下一个 VC 的方法)

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [self.modelArray indexOfObject:viewController];
    if(currentIndex == self.modelArray.count - 1)
        return nil;

    UIViewController *cVC = [self.modelArray objectAtIndex:currentIndex + 1];
    return cVC;
}

但是当我构建并运行时,我得到以下错误。

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483648 beyond bounds [0 .. 3]'

任何人都可以帮助我吗?

亲切的问候.. 编辑

 thread #1: tid = 0x2403, 0x37776350 libsystem_kernel.dylib`__pthread_kill + 8, stop reason = signal SIGABRT
    frame #0: 0x37776350 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x33115122 libsystem_c.dylib`pthread_kill + 58
    frame #2: 0x33151972 libsystem_c.dylib`abort + 94
    frame #3: 0x36f2fd4e libc++abi.dylib`abort_message + 74
    frame #4: 0x36f2cff8 libc++abi.dylib`default_terminate() + 24
    frame #5: 0x3165ba76 libobjc.A.dylib`_objc_terminate() + 146
    frame #6: 0x36f2d07a libc++abi.dylib`safe_handler_caller(void (*)()) + 78
    frame #7: 0x36f2d114 libc++abi.dylib`std::terminate() + 20
    frame #8: 0x36f2e598 libc++abi.dylib`__cxa_rethrow + 88
    frame #9: 0x3165b9d0 libobjc.A.dylib`objc_exception_rethrow + 12
    frame #10: 0x38c82f20 CoreFoundation`CFRunLoopRunSpecific + 456
    frame #11: 0x38c82d48 CoreFoundation`CFRunLoopRunInMode + 104
    frame #12: 0x34bbc2ea GraphicsServices`GSEventRunModal + 74
    frame #13: 0x35c14300 UIKit`UIApplicationMain + 1120
    frame #14: 0x0005050c Franke2`main(argc=1, argv=0x2fdb1d20) + 116 at main.m:16
    frame #15: 0x340ccb20 libdyld.dylib`start + 4
4

3 回答 3

2

2147483648 是 NSNotFound,-[NSArray indexOfObject:]如果找不到对象则返回。

所以在你的行中

    NSUInteger currentIndex = [self.modelArray indexOfObject:viewController];

viewController不在modelArray。这是因为您每次都在创建视图控制器的新实例。

附带说明一下,您正在self.modelArray = [[NSMutableArray alloc] init];对该数组执行然后什么也不做,并在您self.modelArray = pageData;. 当您只使用一个时,无需在那里创建两个空数组。

于 2013-03-13T14:28:36.047 回答
0

调用该方法后,indexOfObject:您应该验证返回的索引不是NSNotFound巫婆是返回的值是对象不在数组中。

你的代码应该看起来更像这样;

NSUInteger currentIndex = [self.modelArray indexOfObject:viewController];
if(currentIndex == NSNotFound ||
   currentIndex == self.modelArray.count - 1)
    return nil;

将来您应该能够通过使用Xcode 异常断点自己找到这些错误。

于 2013-03-13T14:32:50.327 回答
0
 ViewOneViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewOne"];
 NSArray *viewControllers = [NSArray arrayWithObject:cVC];
 [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

在这里,您正在创建 ViewOneViewController 的新对象并添加到 pageViewController 的 viewControllers 数组中。这个 cVC 对象与您之前在 modelArray 中添加的对象无关。它们都是同一类的不同对象,即 ViewOneViewController。

因此,您必须将相同的对象从 modelArray 传递到 pageViewController 的 viewControllers 数组,这样您就不会得到垃圾值

NSUInteger currentIndex = [self.modelArray indexOfObject:viewController]; //will return garbage value in your case

从 instanceViewControllerWithIdentifier 的讨论中

每次调用此方法时,都会创建指定视图控制器的新实例。

于 2013-03-13T14:27:52.243 回答