我试图从 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”正确设置为一个类。 在按照激活断点导航器的建议(我将其设置为“所有异常”'Break on Throw')后,运行在主要部分的这一行停止('return 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)
最佳答案
错误消息说您正在尝试插入一个 nil 对象。我要检查的第一件事是 Storyboard中的 View Controller 是否设置了正确的标识符(“VC1”...等)。
关于ios - UIPageViewController 索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836297/