代码说起来也不简单,不过也不复杂,类是UITableView 通过datasource与delegate委托获取数据源和操作.
下面直接上代码:
CWPageModel.h
#import <Foundation/Foundation.h>
@class CWPageController;
@interface CWPageModel : NSObject <UIPageViewControllerDataSource>
@property (strong, nonatomic) NSArray *pageContent;
- (CWPageController *)viewControllerAtIndex:(NSUInteger)index;
- (NSUInteger)indexOfViewController:(CWPageController *)viewController;
@end
CWPageModel.m
#import "CWPageModel.h"
#import "CWPageController.h"
@implementation CWPageModel
@synthesize pageContent;
- (void) createContentPages {
NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
for (int i = 1; i < 11; i++)
{
NSString *contentString = [[NSString alloc]
initWithFormat:@"This is the page %d of content displayed using UIPageViewController in iOS 5.", i];
[pageStrings addObject:contentString];
}
pageContent = [[NSArray alloc] initWithArray:pageStrings];
}
- (id) init {
if (self = [super init]) {
[self createContentPages];
}
return self;
}
- (void) dealloc {
[pageContent release];
[super dealloc];
}
- (CWPageController *)viewControllerAtIndex:(NSUInteger)index {
// Return the data view controller for the given index.
if (([self.pageContent count] == 0) ||
(index >= [self.pageContent count])) {
return nil;
}
// Create a new view controller and pass suitable data.
CWPageController *dataViewController =
[[[CWPageController alloc]
init] autorelease];
dataViewController.dataObject =
[self.pageContent objectAtIndex:index];
return dataViewController;
}
- (NSUInteger)indexOfViewController:(CWPageController *)viewController {
return [self.pageContent
indexOfObject:viewController.dataObject];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:
(CWPageController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:
(CWPageController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageContent count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
@end
CWPageController.h
#import <UIKit/UIKit.h>
#pragma mark -
#pragma mark pageController
@interface CWPageController : UIViewController
@property (strong, nonatomic) id dataObject;
@end
CWPageController.m
#import "CWPageController.h"
@interface CWPageController ()
@end
@implementation CWPageController
@synthesize dataObject;
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
label.text = dataObject;
label.numberOfLines = 5;
[self.view addSubview:label];
[label release];
}
@end
#import <UIKit/UIKit.h>
#import "CWPageController.h"
#import "CWPageModel.h"
#define BARBUTTONITEM(title,act) [[[UIBarButtonItem alloc] initWithTitle:title \
style:UIBarButtonItemStylePlain \
target:self \
action:act] autorelease];
//--------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark ViewController()
@interface ViewController : UIViewController <UIPageViewControllerDelegate> {
}
@property (nonatomic,retain) UIPageViewController *pageController;
@property (nonatomic,retain) CWPageModel *pModel;
@end
@implementation ViewController
@synthesize pageController = _pageController;
@synthesize pModel = _pModel;
- (void) dealloc {
[_pageController release];
[_pModel release];
[super dealloc];
}
- (void) viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"demo";
_pModel = [[CWPageModel alloc] init];
NSDictionary *options =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
forKey: UIPageViewControllerOptionSpineLocationKey];
_pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationVertical
options:options];
CWPageController *initialViewController =
[_pModel viewControllerAtIndex:0];
NSArray *viewControllers =
[NSArray arrayWithObject:initialViewController];
[_pageController setDataSource:_pModel];
[_pageController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionReverse
animated:NO
completion:^(BOOL f){}];
[self addChildViewController:_pageController];
[self.view addSubview:_pageController.view];
[_pageController didMoveToParentViewController:self];
}
- (void) viewDidUnload {
[super viewDidUnload];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
@end
//-----------------------------------------------------------------------------------------------------
#pragma mark -
#pragma mark AppDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void) dealloc {
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
srand(time(NULL));
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] init] autorelease];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = controller;
[controller release];
[self.window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}