2013-05-30 84 views
3

我需要幫助UIpageviewcontroller以獲得通用應用程序。我在我的應用程序中添加了這項功能,但它工作正常,但是當我在橫向模式下第一次加載UIpageviewcontroller時,它僅顯示單一視圖。我的問題是,如果設備處於橫向模式,它只會在第一次加載單面視圖。我認爲它是因爲UIpageviewcontroller的聲明代碼。UIpageviwcontroller在第一次加載視圖時僅在橫向模式下加載單個視圖

碼 - :

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; 

//任何一個可以幫助我解決這個問題。

我很感激你提前幫忙。

回答

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和ContentViewController.h文件連接插座web視圖。

就是這樣..

這裏是輸出

enter image description here

+0

,但是當我把這個線獲取當前指數它給空數組錯誤不知道爲什麼數組是空的 - :[[自我。 Pagecontrol viewControllers] objectAtIndex:0];如果我設置單個控制器它工作正常 – Anjan

+0

在你的代碼我面對這條線上的錯誤NSUInteger索引= [self indexOfViewController: (contentViewController *)viewController];在這一行上,我得到了viewcontroller的空數組 – Anjan

+0

等幾分鐘我給了一個樣本 – Warewolf

相關問題