PVCViewController.h
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
@interface PVCViewController : UIViewController<UIPageViewControllerDataSource>
{
UIPageViewController *pageController;
NSArray *pageContent;
}
@property (strong, nonatomic) UIPageViewController *pageController;
@property (strong, nonatomic) NSArray *pageContent;
@end
PVCViewController.m
#import "PVCViewController.h"
@interface PVCViewController()
@end
@implementation PVCViewController
@synthesize pageController, pageContent;
- (void) createContentPages
{
NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
for (int i = 1; i < 11; i++)
{
NSString *contentString = [[NSString alloc]
initWithFormat:@"<html><head></head><body><h1>Chapter %d</h1><p>This is the page %d of content displayed using UIPageViewController in iOS 5.</p></body></html>", i, i];
[pageStrings addObject:contentString];
}
pageContent = [[NSArray alloc] initWithArray:pageStrings];
}
- (ContentViewController*)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.
ContentViewController *dataViewController =
[[ContentViewController alloc]initWithNibName:@"ContentViewController" bundle:nil];
dataViewController.dataObject =[self.pageContent objectAtIndex:index];
return dataViewController;
}
-(NSUInteger)indexOfViewController:(ContentViewController *)viewController
{
return [self.pageContent indexOfObject:viewController.dataObject];
}
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(ContentViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:
(ContentViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageContent count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createContentPages];
NSDictionary *options =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options: options];
self.pageController.doubleSided=YES;
pageController.dataSource = self;
[[pageController view] setFrame:[[self view] bounds]];
NSArray *viewControllers =[NSArray arrayWithObjects:[self viewControllerAtIndex:0],[self viewControllerAtIndex:1],nil];
[pageController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:pageController];
[[self view] addSubview:[pageController view]];
[pageController didMoveToParentViewController:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ContentViewController.h
#import <UIKit/UIKit.h>
@interface ContentViewController : UIViewController
{
UIPageViewController *pageController;
NSArray *pageContent;
}
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) id dataObject;
@end
ContentViewController.m
#import "ContentViewController.h"
@interface ContentViewController()
@end
@implementation ContentViewController
@synthesize webView, dataObject;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[webView loadHTMLString:dataObject
baseURL:[NSURL URLWithString:@""]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
添加的WebView到廈門國際銀行contentViewController和ContentViewController.h文件連接插座web視圖。
就是這樣..
這裏是輸出
,但是當我把這個線獲取當前指數它給空數組錯誤不知道爲什麼數組是空的 - :[[自我。 Pagecontrol viewControllers] objectAtIndex:0];如果我設置單個控制器它工作正常 – Anjan
在你的代碼我面對這條線上的錯誤NSUInteger索引= [self indexOfViewController: (contentViewController *)viewController];在這一行上,我得到了viewcontroller的空數組 – Anjan
等幾分鐘我給了一個樣本 – Warewolf