2016-08-15 15 views
0

UIPageViewControllerを初めて使用していて、Objective-Cにその説明がありません。私は自分で実装しようとしましたが、起動時に黒い画面しか表示されません。私は間違って何をしていますか?UIPageView実装の黒い画面

私は初期のViewControllerであるIBで設定されたViewControllerと、以下の2つのストーリーボードIDを持ち、何かに接続されずにそこに座っている2つのものを持っています。ここで

は初期のViewControllerのために私のコードです:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    [self addChildViewController:self.pageViewController]; 
    self.pageViewController.delegate = self; 
    self.pageViewController.dataSource = self; 

    UIViewController *vc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"]; 
    UIViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"]; 

    viewControllers = [[NSArray alloc] initWithObjects:vc1,vc2, nil]; 

    [self.pageViewController setViewControllers:@[vc1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

} 

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    return viewControllers[index]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
    viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [viewControllers indexOfObject:viewController]; 

    --currentIndex; 
    currentIndex = currentIndex % (viewControllers.count); 
    return [viewControllers objectAtIndex:currentIndex]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [viewControllers indexOfObject:viewController]; 

    ++currentIndex; 
    currentIndex = currentIndex % (viewControllers.count); 
    return [viewControllers objectAtIndex:currentIndex]; 
} 

-(NSInteger)presentationCountForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return viewControllers.count; 
} 

-(NSInteger)presentationIndexForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 
+0

あなたはこれをチェックすることができます - https://github.com/mindsnacks/MSPageViewControllerを多分助けること。 – Ro4ch

+1

pageViewControllerを画面に追加した時点が表示されません。 –

+0

ここにあります:[self addChildViewController:self.pageViewController]; –

答えて

0

あなたは

addSubview

が欠落している、その後、あなたは

didMoveToParentViewConを呼び出す必要があります子コントローラのライフサイクルイベントを取得するためのコントローラ

@interface ViewController()<UIPageViewControllerDelegate, UIPageViewControllerDataSource> 
@property (nonatomic, strong) UIPageViewController* pageViewController; 
@property (nonatomic,strong) NSArray *viewControllers; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    [self addChildViewController:self.pageViewController]; 
    [self.view addSubview:self.pageViewController.view]; 
    [self.pageViewController didMoveToParentViewController:self]; 
    self.pageViewController.delegate = self; 
    self.pageViewController.dataSource = self; 

    UIViewController *vc1 = [[UIViewController alloc] init]; 
    vc1.view.backgroundColor = [UIColor blueColor]; 
    UIViewController *vc2 = [[UIViewController alloc] init]; 
    vc2.view.backgroundColor = [UIColor redColor]; 

    self.viewControllers = [[NSArray alloc] initWithObjects:vc1,vc2, nil]; 

    [self.pageViewController setViewControllers:@[vc1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

} 

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    return self.viewControllers[index]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
    viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.viewControllers indexOfObject:viewController]; 

    --currentIndex; 
    currentIndex = currentIndex % (self.viewControllers.count); 
    return [self.viewControllers objectAtIndex:currentIndex]; 
} 

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.viewControllers indexOfObject:viewController]; 

    ++currentIndex; 
    currentIndex = currentIndex % (self.viewControllers.count); 
    return [self.viewControllers objectAtIndex:currentIndex]; 
} 

-(NSInteger)presentationCountForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return self.viewControllers.count; 
} 

-(NSInteger)presentationIndexForPageViewController: 
(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 
@end 
+0

@BalázsVinczeあなたは私の答えを試しましたか? – MCMatan

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 


    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 

    self.pageController.dataSource = self; 
    self.pageController.delegate = self; 

    [NSTimer scheduledTimerWithTimeInterval:5.0 
            target:self 
            selector:@selector(loadNextController) 
            userInfo:nil 
            repeats:YES]; 


    [[self.pageController view] setFrame:[[self sliderView] bounds]]; 

    self.pageControl.numberOfPages = 5; 
    self.pageControl.currentPage = 0; 

    PageChildVC *initialViewController = [self viewControllerAtIndex:0]; 

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController]; 

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    [self addChildViewController:self.pageController]; 
    [[self sliderView] addSubview:[self.pageController view]]; 
    [self.pageController didMoveToParentViewController:self]; 
} 


- (PageChildVC *)viewControllerAtIndex:(NSUInteger)index { 
    if ((5 == 0) || (index >= 5)) { 
     return nil; 
    } 
    PageChildVC *childViewController = [[PageChildVC alloc] initWithNibName:@"PageChildVC" bundle:nil]; 
    childViewController.index = index; 
    return childViewController; 

} 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 
    NSUInteger index = ((PageChildVC*) viewController).index; 
    if ((index == 0) || (index == NSNotFound)) { 
     index = 5; 
    } 
    index--; 
    return [self viewControllerAtIndex:index]; 
    } 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 
    NSUInteger index = ((PageChildVC*) viewController).index; 
    if (index == NSNotFound) { 
     return nil; 
    } 
    index++; 
    if (index == 5) { 
     index = 0; 
    } 
    return [self viewControllerAtIndex:index]; 
    } 
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { 
    return 5; 
} 
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { 
    return [(PageChildVC *)[self.pageController.viewControllers firstObject] index]; 
} 
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed 
{ 
    PageChildVC *currentViewController = pageViewController.viewControllers[0]; 
    currentIndex = (int)currentViewController.index; 
    [self.pageControl setCurrentPage:currentIndex]; 
} 
- (void)loadNextController { 
    currentIndex++; 
    PageChildVC *nextViewController = [self viewControllerAtIndex:currentIndex]; 
    if (nextViewController == nil) { 
     currentIndex = 0; 
     nextViewController = [self viewControllerAtIndex:currentIndex]; 
    } 
    [self.pageController setViewControllers:@[nextViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
    [self.pageControl setCurrentPage:currentIndex]; 
}