UIPageView实现黑屏

时间:2016-08-15 12:40:44

标签: ios objective-c uipageviewcontroller

我不熟悉使用UIPageViewController,但在Objective-C中找不到合适的解释。我尝试自己实现一个,但它只在启动时显示黑屏。我做错了什么?

我在IB中设置了一个ViewController,它是最初的ViewController,另外两个在下面有两个故事板ID,只是坐在那里,没有连接任何东西。

这是我的初始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;
}

2 个答案:

答案 0 :(得分:0)

你错过了

  

addSubview

之后,你应该打电话给

  

didMoveToParentViewController

用于在子视图控制器上获取生命周期事件。

@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

答案 1 :(得分: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];
}