UIpageviewcontroller在第一次加载视图时仅在横向模式下加载单个视图

时间:2013-05-30 11:20:44

标签: iphone objective-c ipad ios5

我需要UIpageviewcontroller的帮助以进行全面应用。我在我的应用程序中添加此功能它工作正常,但当我第一次在横向模式下加载UIpageviewcontroller时,它只显示单个视图。我的问题是如果设备处于横向模式,它第一次只加载sigle视图。我认为这是因为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;

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

我感谢您提前帮助。

1 个答案:

答案 0 :(得分: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

<强> 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 xib并将插件连接到ContentViewController.h文件中的webView。

就是这样..

这是输出

enter image description here