简体   繁体   English

UIPageViewController索引错误

[英]UIPageViewController index error

I am trying to open 3 ViewControllers ("VC1", "VC2" "VC3") from a UIPageViewController but I get errors which I don't understand. 我正在尝试从UIPageViewController打开3个ViewControllers ("VC1", "VC2" "VC3") ,但是出现了我不理解的错误。
I am not very familiar with x-code yet. 我对x代码不是很熟悉。

This is my code from PagingViewController.h : 这是我来自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

This is the code from "PagingViewController.m": 这是来自“ 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

And this is the error report from the console: 这是来自控制台的错误报告:

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

Thanks a lot for helping! 非常感谢您的帮助! Daniel 丹尼尔

UPDATE UPDATE

Meanwhile I have set up 'VC1' properly as a class. 同时,我已经适当地将“ VC1”设置为一个类。 And after following the advice to activate the Breakpoint navigator (which I set up as "All Exceptions" 'Break on Throw') the Run stopped at this line in the main section ('return UIApplication ... see below) 在遵循了建议以激活Breakpoint导航器(我将其设置为“所有异常”“ Threak on Throw”)之后,Run在主要部分的这一行停止了(“ return UIApplication ...见下文)”

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

    }

This is the new error message in the console: 这是控制台中的新错误消息:

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) 

The error message says you're trying to insert a nil object. 错误消息表明您正在尝试插入一个nil对象。 First thing I'd check is whether the view controllers in the storyboard are set up with the correct identifiers ('VC1'... etc). 我首先要检查的是情节提要中的视图控制器是否设置了正确的标识符('VC1'...等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM