嗨,这是我第一次尝试使用UIPageViewController。我遵循了this教程,由于某种原因,PageContentViewController永远不会加载。我知道我可能会遗漏一些非常愚蠢的东西,所以我会欣赏一双清新的眼睛。
以下是我的故事板的截图:
这是充当PageViewController数据源的类
//
// ViewController.m
// Pregrancy
//
// Created by Andreas Kaitis on 24/10/14.
// Copyright (c) 2014 Andreas Kaitis. All rights reserved.
//
#import "ViewController.h"
#import "PageContentViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_pageImages = [[NSArray alloc]initWithObjects: @"week1", @"week2", @"week3", @"week4",
@"week5", @"week6", @"week7", @"week8",
@"week9", @"week10",@"week11",@"week12",
@"week13",@"week14",@"week15",@"week16",
@"week17",@"week18",@"week19",@"week20",
@"week21",@"week22",@"week23",@"week24",
@"week25",@"week26",@"week27",@"week28",
@"week29",@"week30",@"week31",@"week32",
@"week37",@"week38",@"week39",@"week40", nil];
_pageTitles = [[NSArray alloc]initWithObjects: @"Month 1", @"Month 1", @"Month 1", @"Month 1",
@"Month 2", @"Month 2", @"Month 2", @"Month 2",
@"Month 3", @"Month 3", @"Month 3", @"Month 3",
@"Month 4", @"Month 4", @"Month 4", @"Month 4",
@"Month 5", @"Month 5", @"Month 5", @"Month 5",
@"Month 6", @"Month 6", @"Month 6", @"Month 6",
@"Month 7", @"Month 7", @"Month 7", @"Month 7",
@"Month 8", @"Month 8", @"Month 8", @"Month 8",
@"Month 9", @"Month 9", @"Month 9", @"Month 9",nil];
_pageSubtitles = [[NSArray alloc]initWithObjects:@"week1", @"week2", @"week3", @"week4",
@"week5", @"week6", @"week7", @"week8",
@"week9", @"week10", @"week11", @"week12",
@"week13", @"week14", @"week15", @"week16",
@"week17", @"week18", @"week19", @"week20",
@"week21", @"week22", @"week23", @"week24",
@"week25", @"week26", @"week27", @"week28",
@"week29", @"week30", @"week31", @"week32",
@"week33", @"week34", @"week35", @"week36",nil];
_pageBodyText = [[NSArray alloc]initWithObjects:@"week1", @"week2", @"week3", @"week4",
@"week5", @"week6", @"week7", @"week8",
@"week9", @"week10", @"week11", @"week12",
@"week13", @"week14", @"week15", @"week16",
@"week17", @"week18", @"week19", @"week20",
@"week21", @"week22", @"week23", @"week24",
@"week25", @"week26", @"week27", @"week28",
@"week29", @"week30", @"week31", @"week32",
@"week33", @"week34", @"week35", @"week36",nil];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.subtitleText = self.pageSubtitles[index];
pageContentViewController.bodyText = self.pageBodyText[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageTitles count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
@end
和PageContentViewController
#import "PageContentViewController.h"
@interface PageContentViewController () <UITableViewDataSource, UITableViewDataSource>
@end
@implementation PageContentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:self.imageFile];
self.monthLabel.text = self.titleText;
self.weekLabel.text = self.subtitleText;
self.bodyTextLabel.text = self.bodyText;
self.monthVisualEffectView.hidden = YES;
self.weekVisualEffectView.hidden = YES;
self.months = [[NSArray alloc]initWithObjects:@"Month 1",@"Month 2",@"Month 3",@"Month 4",@"Month 5",@"Month 6",@"Month 7",@"Month 8",@"Month 9", nil];
self.weeks = [[NSArray alloc]initWithObjects: @"week 1", @"week 2", @"week 3", @"week 4",
@"week 5", @"week 6", @"week 7", @"week 8",
@"week 9", @"week 10", @"week 11", @"week 12",
@"week 13", @"week 14", @"week 15", @"week 16",
@"week 17", @"week 18", @"week 19", @"week 20",
@"week 21", @"week 22", @"week 23", @"week 24",
@"week 25", @"week 26", @"week 27", @"week 28",
@"week 29", @"week 30", @"week 31", @"week 32",
@"week 33", @"week 34", @"week 35", @"week 36",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}