2014-02-17 72 views
1

我想從UIPageViewController打開3 ViewControllers("VC1", "VC2" "VC3")但我得到我不明白的錯誤。
我對x代碼還不是很熟悉。UIPageViewController索引錯誤

這是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 

非常感謝您的幫助! 丹尼爾

UPDATE

同時,我已經設置了 'VC1' 正常的一類。 並且在按照建議激活斷點導航器(我將其設置爲「所有例外」「拋出中斷」)之後,運行停止在主要部分中的此行('return UIApplication ... see below)

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) 
+0

您確定VC1或VC2或VC3不是零嗎?去看一下。你也可以添加一個異常斷點來查看哪一行導致錯誤。 –

+0

@EPyLEpSY VC1沒有正確設置爲類。我同時做了這件事。現在控制檯中有一個不同的錯誤消息。我已經將它作爲UPDATE複製到我的原始問題中。再次感謝! – user3156814

回答

1

錯誤消息說你要插入一個零對象。首先我要檢查的是故事板中的視圖控制器是否設置了正確的標識符('VC1'...等)。

+0

感謝您的諮詢!看來我現在走在了正確的軌道上。但是,我無法理解新的錯誤警告。 (請參閱我的原始消息中的更新。謝謝,丹尼爾 – user3156814

+0

錯誤是完全一樣的...你試圖插入一個無對象到數組中.... – CW0007007

+0

檢查所有視圖控制器不是零 – CW0007007