【问题标题】:UIPageViewController index errorUIPageViewController 索引错误
【发布时间】:2014-02-17 18:09:40
【问题描述】:

我正在尝试从 UIPageViewController 打开 3 ViewControllers ("VC1", "VC2" "VC3"),但我遇到了我不明白的错误。
我对x-code还不是很熟悉。

这是我来自PagingViewController.h的代码:

#import <UIKit/UIKit.h>

@interface PagingViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (assign, nonatomic) NSInteger index;

@property int defaultIndex;

@property NSArray *viewControllers;
@property UIPageControl *pageController;
@property UIPageViewController *pageViewController;

@property UIViewController *VC1;
@property UIViewController *VC2;
@property UIViewController *VC3;

@end

这是来自“PagingViewController.m”的代码:

#import "PagingViewController.h"

@interface PagingViewController ()

@end

@implementation PagingViewController



- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {


//    self.index--;
//    if(self.index<0)
//    {
//        self.index=0;
//    }
    return [self viewControllerAtIndex:self.index--];

}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    //self.index++;

    return [self viewControllerAtIndex:self.index++];

}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {

    return self.viewControllers[index];


}



- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    return 3;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return self.defaultIndex;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;

    self.index = 1;

    self.VC1 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC1"];
    self.VC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC2"];
    self.VC3 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC3"];

    self.viewControllers = @[self.VC1, self.VC2, self.VC3];

    //self.index = 1;

    //  [self performSelector:@selector(loadingNextView) withObject:nil afterDelay:2.0f];

    [self.pageViewController setViewControllers:self.viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self addChildViewController:self.pageViewController];

    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

    //NSArray *viewControllers = [NSArray arrayWithObject:self.VC1];
    //[self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这是来自控制台的错误报告:

2014-02-17 18:34:05.560 PageViewC und TableVC-20140217[4493:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173c5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bf8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x016eebcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x016ee870 -[__NSArrayM addObject:] + 64
    4   UIKit                               0x00343d7b -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 417
    5   UIKit                               0x00355874 -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 68
    6   PageViewC und TableVC-20140217      0x00002b4c -[PagingViewController viewDidLoad] + 1180
    7   UIKit                               0x00341318 -[UIViewController loadViewIfRequired] + 696
    8   UIKit                               0x003415b4 -[UIViewController view] + 35
    9   UIKit                               0x002699fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    10  UIKit                               0x00269d97 -[UIWindow _setHidden:forced:] + 312
    11  UIKit                               0x0026a02d -[UIWindow _orderFrontWithoutMakingKey] + 49
    12  UIKit                               0x0027489a -[UIWindow makeKeyAndVisible] + 65
    13  UIKit                               0x00227cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    14  UIKit                               0x0022c3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    15  UIKit                               0x0024087c -[UIApplication handleEvent:withNewEvent:] + 3447
    16  UIKit                               0x00240de9 -[UIApplication sendEvent:] + 85
    17  UIKit                               0x0022e025 _UIApplicationHandleEvent + 736
    18  GraphicsServices                    0x036e32f6 _PurpleEventCallback + 776
    19  GraphicsServices                    0x036e2e01 PurpleEventCallback + 46
    20  CoreFoundation                      0x016b7d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    21  CoreFoundation                      0x016b7a9b __CFRunLoopDoSource1 + 523
    22  CoreFoundation                      0x016e277c __CFRunLoopRun + 2156
    23  CoreFoundation                      0x016e1ac3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x016e18db CFRunLoopRunInMode + 123
    25  UIKit                               0x0022badd -[UIApplication _run] + 840
    26  UIKit                               0x0022dd3b UIApplicationMain + 1225
    27  PageViewC und TableVC-20140217      0x000033dd main + 141
    28  libdyld.dylib                       0x01d7a70d start + 1
    29  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

非常感谢您的帮助! 丹尼尔

更新

同时,我已将“VC1”正确设置为一个类。 在按照建议激活断点导航器(我将其设置为“所有异常”“抛出时中断”)之后,运行在主要部分的这一行停止(“返回 UIApplication ... 见下文)

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }

这是控制台中的新错误消息:

2014-02-18 17:23:02.967 PageViewC und TableVC-20140217[5907:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173c5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bf8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x016eebcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x016ee870 -[__NSArrayM addObject:] + 64
    4   UIKit                               0x00343d7b -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 417
    5   UIKit                               0x00355874 -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 68
    6   PageViewC und TableVC-20140217      0x00002acc -[PagingViewController viewDidLoad] + 1180
    7   UIKit                               0x00341318 -[UIViewController loadViewIfRequired] + 696
    8   UIKit                               0x003415b4 -[UIViewController view] + 35
    9   UIKit                               0x002699fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    10  UIKit                               0x00269d97 -[UIWindow _setHidden:forced:] + 312
    11  UIKit                               0x0026a02d -[UIWindow _orderFrontWithoutMakingKey] + 49
    12  UIKit                               0x0027489a -[UIWindow makeKeyAndVisible] + 65
    13  UIKit                               0x00227cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    14  UIKit                               0x0022c3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    15  UIKit                               0x0024087c -[UIApplication handleEvent:withNewEvent:] + 3447
    16  UIKit                               0x00240de9 -[UIApplication sendEvent:] + 85
    17  UIKit                               0x0022e025 _UIApplicationHandleEvent + 736
    18  GraphicsServices                    0x036e32f6 _PurpleEventCallback + 776
    19  GraphicsServices                    0x036e2e01 PurpleEventCallback + 46
    20  CoreFoundation                      0x016b7d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    21  CoreFoundation                      0x016b7a9b __CFRunLoopDoSource1 + 523
    22  CoreFoundation                      0x016e277c __CFRunLoopRun + 2156
    23  CoreFoundation                      0x016e1ac3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x016e18db CFRunLoopRunInMode + 123
    25  UIKit                               0x0022badd -[UIApplication _run] + 840
    26  UIKit                               0x0022dd3b UIApplicationMain + 1225
    27  PageViewC und TableVC-20140217      0x0000335d main + 141
    28  libdyld.dylib                       0x01d7a70d start + 1
    29  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

  • 您确定 VC1 或 VC2 或 VC3 不是 nil 吗?去看一下。您还可以添加异常断点以查看导致错误的行。
  • @EPyLEpSY VC1 未正确设置为类。这期间我已经做到了。现在控制台中出现了不同的错误消息。我已将其作为更新复制到我的原始问题中。再次感谢!

标签: ios objective-c uipageviewcontroller


【解决方案1】:

错误消息表明您正在尝试插入一个 nil 对象。我首先要检查的是情节提要中的视图控制器是否设置了正确的标识符('VC1'...等)。

【讨论】:

  • 感谢您的建议!看来我现在走在正确的轨道上。但是,我无法理解新的错误警告。 (请参阅我原始消息中的更新。谢谢,丹尼尔
  • 错误是完全一样的...你正在尝试将一个 nil 对象插入到一个数组中......
  • 检查所有视图控制器不为零。
  • @CW0007007 谢谢!如何确定视图控制器是否不为零?
  • 添加一个断点并单步检查它们在调试器中的值...?
猜你喜欢
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多