3

我需要帮助以UIpageviewcontroller进行通用应用。我在我的应用程序中添加了这个功能,它工作正常,但是当我UIpageviewcontroller第一次在横向模式下加载时,它只显示单个视图。我的问题是,如果设备处于横向模式,它第一次只加载一个视图。我认为是因为UIpageviewcontroller.

代码 -:

self.Pagecontrol = [[UIPageViewController alloc] initWithTransitionStyle:style
 navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];



    self.Pagecontrol.delegate = self;

    self.Pagecontrol.dataSource = self;


    //Step 3:
    //Set the initial view controllers.

    PageCurlView *contentViewController = [[PageCurlView alloc] init];

       NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
    // &&& i add single view controller at time of declaration.   

    CGRect pageViewRect = CGRectMake(self.view.frame.origin.x+15, self.view.frame.origin.y+15, self.view.frame.size.width-45, self.view.frame.size.height-35);
    pageViewRect = CGRectInset(pageViewRect, 5.0, 20.0);
    self.Pagecontrol.view.frame = pageViewRect;


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


    //Step 4:
    //ViewController containment steps
    //Add the pageViewController as the childViewController
    [self addChildViewController:self.Pagecontrol];

    //Add the view of the pageViewController to the current view


    [self.BaseScrollView addSubview:self.Pagecontrol.view];
    [self.view addSubview:self.BaseScrollView];

    [self.Pagecontrol.view bringSubviewToFront:self.BaseScrollView];


    [self.Pagecontrol didMoveToParentViewController:self];

    //Step 5:
    // set the pageViewController's frame as an inset rect.


    //Step 6:
    //Assign the gestureRecognizers property of our pageViewController to our view's gestureRecognizers property.

 self.Pagecontrol.view.gestureRecognizers = self.Pagecontrol.gestureRecognizers;

//任何人帮助我解决这个问题。

感谢您提前提供帮助。

4

1 回答 1

0

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

内容视图控制器.h

#import <UIKit/UIKit.h>

@interface ContentViewController : UIViewController
{
    UIPageViewController *pageController;
    NSArray *pageContent;
}
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) id dataObject;
@end

内容视图控制器.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 xib 并将插座连接到 ContentViewController.h 文件中的 webView。

而已..

这是输出

在此处输入图像描述

于 2013-05-30T12:14:05.830 回答