2014-04-11 213 views
-1

我有一個- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion在一個方法中設置。無論何時調用該方法,都不會進入下一頁。相反,它進入UIPageViewController故事板,然後崩潰。我不確定我做錯了什麼。我正在使用MSPageViewController作爲pageviewcontroller,可以嗎?UIPageViewController setViewController導致應用程序崩潰

繼承人我的代碼:

UIViewController *viewcont = [[UIViewController alloc]init]; 

NSArray *viewControllers = [NSArray arrayWithObject:viewcont]; 

[self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

感謝。

有3個故事板全部符合MSPageViewControllerChild且合成了pageIndex屬性。 IntroPageViewController是第一個故事板(p1)。

PagingViewController.h:

// 
// PagingViewController.m 
// MordechaiLevi 
// 
// Created by Mordechai Levi on 4/10/14. 
// Copyright (c) 2014 Mordechai Levi. All rights reserved. 
// 

#import "PagingViewController.h" 
#import "IntroPageViewController.h" 
#import "MSPageViewController.h" 

@interface PagingViewController() 

@end 

@implementation PagingViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.device = [UIDevice currentDevice]; 

    self.device.proximityMonitoringEnabled = YES; 

    if (self.device.proximityMonitoringEnabled == YES) { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorCovered) name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; 

    }else{ 

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Uh Oh!" message:@"To use this app you need a device with a proximity sensor." delegate:self cancelButtonTitle:@"Got it" otherButtonTitles:nil, nil]; 

     [alert show]; 
    } 

    self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.859 blue:0.643 alpha:1.0]; 

} 


- (UIStatusBarStyle)preferredStatusBarStyle { 

    return UIStatusBarStyleLightContent; 
} 

- (void)sensorCovered { 

    if (self.device.proximityState == YES) { 

     IntroPageViewController *viewcont = [[IntroPageViewController alloc]init]; 

     NSArray *viewControllers = [NSArray arrayWithObject:viewcont]; 

     [self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 


     NSLog(@"sensor covered"); 

    }else{ 

     NSLog(@"sensor not covered"); 
    } 
} 




- (NSArray *)pageIdentifiers { 

    return @[@"p1", @"p2", @"p3"]; 
} 


@end 
+0

你可以添加崩潰日誌嗎? – shannoga

+0

@shannoga只是將其添加到問題 – user3186310

+1

添加異常斷點並查看崩潰發生的位置。 –

回答

1

看起來你正在使用MSPageViewController,與不符合MSPageViewControllerChild控制器。

從文檔:

他們每個人[控制器]的必須是符合MSPageViewControllerChild(如果你不需要任何額外的功能添加到它,你可以使用MSPageViewControllerPage)類。

+0

我的所有頁面都符合'MSPageViewControllerChild'或正在使用'MSPageViewCotrollerPage'。我的故事板是「MSPageViewControllerChild」和「MSPageViewCotrollerPage」的混合體。真的很感謝答案,謝謝 – user3186310

+0

你正在合成'pageIndex'屬性嗎? – NachoSoto

+0

是的,我只是改變UIViewController UIViewController * viewcont = [[UIViewController alloc] init];轉換爲符合MSPageViewControllerChild的類。該應用程序不會崩潰,但不會進入下一頁。它只是進入一個空白頁面(從我瞭解UIPageViewController故事板)。謝謝。 – user3186310