使用故事板崩溃的UIPageviewController

问题描述:

我要使用UIPageViewController制作教程页面。使用故事板崩溃的UIPageviewController

但是发生了事故。我无法找到发生崩溃的点。

我想直接使用UIPageViwController,而不是在UIViewController中使用。

dataSource works。但是当试图调用[self setViewControllers:@ [pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; ,发生如下错误。

UserGuidePageViewController链接到右舷的UIPagveViewController框架。

以下为部首

#import <UIKit/UIKit.h> 

@interface UserGuidePageViewController : UIPageViewController <UIPageViewControllerDataSource> { 

} 


@end 

以下是代码

#import "UserGuidePageViewController.h" 
#import "UserGuideViewController.h" 


@interface UserGuidePageViewController() { 

} 

@property (nonatomic, retain) NSArray *array; 

@end 

@implementation UserGuidePageViewController 


- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 

    if (self) { 
     self.dataSource = self; 
    } 

    return self; 
} 

- (UserGuideViewController *)userGuideViewForPageIndex:(NSUInteger)pageIndex 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    UserGuideViewController *targetViewController = [storyboard instantiateViewControllerWithIdentifier:@"UserGuideViewController"]; 

    return targetViewController; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UserGuideViewController *pageZero = [self userGuideViewForPageIndex:0]; 

    [self setViewControllers:@[pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 
    --> Crash happened here!!! 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

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

#pragma mark - Page View Controller Data Source 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = [(UserGuideViewController *)viewController pageIndex]; 

    return [self userGuideViewForPageIndex:(index - 1)]; 
} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = [(UserGuideViewController *)viewController pageIndex]; 

    return [self userGuideViewForPageIndex:(index + 1)]; 
} 

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

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

误差以下。

*** Assertion failure in -[_UIQueuingScrollView setView:direction:animated:completion:], /SourceCache/UIKit/UIKit-2903.23/_UIQueuingScrollView.m:671 
2013-12-18 15:23:35.779 Natural Calendar[4497:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view' 
*** First throw call stack: 
(0x30ef6f4b 0x3b6d36af 0x30ef6e25 0x3189efe3 0x33ba3321 0x33b382eb 0x33b38407 0xf4715 0x3366e37b 0x3366e139 0xf0c45 0x1062d9 0x1106c3 0x3369e713 0x3369e6b3 0x3369e691 0x3368a11f 0x3369e107 0x3369ddd9 0x33698e65 0x3366e79d 0x3366cfa3 0x30ec2183 0x30ec1653 0x30ebfe47 0x30e2ac27 0x30e2aa0b 0x35b0b283 0x336ce049 0xecd09 0x3bbdbab7) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

其实我改成了Xcode模板样式。

但是这个错误也发生了。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    [_pageViewController setDataSource:self]; 

    UserGuideViewController *pageZero = [self userGuideViewForPageIndex:0]; 

    [_pageViewController setViewControllers:@[pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

    [self addChildViewController:_pageViewController]; 
    [self.view addSubview:[_pageViewController view]]; 

    CGRect pageViewRect = self.view.bounds; 
    //pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0); 
    _pageViewController.view.frame = pageViewRect; 

    [_pageViewController didMoveToParentViewController:self]; 
} 

我不知道是什么问题。当选择UIPageViewControllerTransitionStyleScroll时,发生错误。当选择UIPageViewControllerTransitionStylePageCurl时,错误不会发生。

+0

你也应该改变你的包从零到'[NSBundle mainBundle]'我认为编辑:对不起,只是阅读文档,如果你指定nil它自动看故事板的mainBundle – dehlen

我在ContentViewController处发现了loadView() { }。但loadView()保持为空而不呼叫[super loadView];。因此,视图将保持nil

现在看来在删除方法loadView()后工作。

@interface UserGuidePageViewController : UIPageViewController <UIPageViewControllerDataSource> { 

chnge与

@interface UserGuidePageViewController : UIViewController <UIPageViewControllerDataSource> { 

此行意味着你可以使用的UIViewController这个

看到this链接以供参考。

和另一个link它是使用故事板。

+0

是否有唯一的方法来使用UIViewController?我在新项目中检查了模板代码。我试图使用UIPageViewController,因为它似乎使用UIPageViewController而没有为UIPageViewController声明属性。 – mkjwa

+0

这[链接](http://*.com/questions/18398796/uipageviewcontroller-and-storyboard?rq=1)似乎直接扩展UIPageViewController,但它有另一个问题。 – mkjwa

+0

对不起亲爱的,我没有使用这个像扩展uipageviewcontroller。 –