2013-04-04 82 views
27

我在iPad上使用UIPageViewController,我需要在第一頁顯示firstviewController,在橫向顯示下一頁顯示ContentViewController如何在UIPageViewcontroller中添加兩個視圖控制器

如果我設置NSArray兩個viewControllers該應用是在[self.pagviewController setViewController:]崩潰有以下例外:

提供視圖控制器的數量(2)不匹配所需要的數目(1)爲請脊骨位置(UIPageViewControllerSpineLocationMin)

下面是代碼:

#pragma mark - UIPageViewControllerDataSource Methods 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController textContents]]; 
    if(currentIndex == 0) 
    { 
     return nil; 
    } 
    ContentViewController *contentViewController = [[ContentViewController alloc] init]; 
    contentViewController.textContents = [self.modelArray objectAtIndex:currentIndex - 1]; 
    return contentViewController; 
} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
     viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController textContents]]; 
    if(currentIndex == self.modelArray.count-1) 
    { 
     return nil; 
    } 
    ContentViewController *contentViewController = [[ContentViewController alloc] init]; 
    contentViewController.textContents = [self.modelArray objectAtIndex:currentIndex + 1]; 

    return contentViewController; 
} 




//#pragma mark - UIPageViewControllerDelegate Methods 

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController 
        spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
    if(UIInterfaceOrientationIsPortrait(orientation)) 
    { 
     //Set the array with only 1 view controller 
     UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 
     NSArray *viewControllers = [NSArray arrayWithObject:currentViewController]; 

     [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 

     //Important- Set the doubleSided property to NO. 
     self.pageViewController.doubleSided = NO; 
     //Return the spine location 
     return UIPageViewControllerSpineLocationMin; 
    } 
    else 
    { 
     NSArray *viewControllers = nil; 
     ContentViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 

     NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)currentViewController textContents]]; 
     if(currentIndex == 0 || currentIndex %2 == 0) 
     { 
      UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController]; 
      viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil]; 
     } 
     else 
     { 
      UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController]; 
      viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil]; 
     } 
     //Now, set the viewControllers property of UIPageViewController 
     [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 

     return UIPageViewControllerSpineLocationMid; 
    } 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; 
    //Instantiate the model array 
    self.modelArray = [[NSMutableArray alloc] init]; 
    self.vcs = [[NSMutableArray alloc]init]; 

    for (int index = 1; index <= 2 ; index++) 
    { 
     [self.modelArray addObject:[NSString stringWithFormat:@"Page %d",index]]; 
    } 

    //Step 1 
    //Instantiate the UIPageViewController. 
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl 
                   navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 
    //Step 2: 
    //Assign the delegate and datasource as self. 
    self.pageViewController.delegate = self; 
    self.pageViewController.dataSource = self; 

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

    appDelegate.contentViewController.textContents = [self.modelArray objectAtIndex:0]; 


    NSArray *viewControllers = [NSArray arrayWithObjects:appDelegate.firstViewController,appDelegate.contentViewController,nil]; 
    [self.pageViewController setViewControllers:viewControllers 
             direction:UIPageViewControllerNavigationDirectionForward 
             animated:NO 
            completion:nil]; 

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

    //Add the view of the pageViewController to the current view 
    [self.view addSubview:self.pageViewController.view]; 

    //Call didMoveToParentViewController: of the childViewController, the UIPageViewController instance in our case. 
    [self.pageViewController didMoveToParentViewController:self]; 

    //Step 5: 
    // set the pageViewController's frame as an inset rect. 
    CGRect pageViewRect = self.view.bounds; 
    pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0); 
    self.pageViewController.view.frame = pageViewRect; 

    //Step 6: 
    //Assign the gestureRecognizers property of our pageViewController to our view's gestureRecognizers property. 
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; 
} 
+1

你需要通過你的數據源提供一次在一個頁面,如果你只是想顯示一個頁面,這就是爲什麼你的應用程序崩潰 - 它有兩個頁面顯示當它期待一個(當我在縱向模式)。向我們顯示您的委託和數據源代碼。 – 2013-04-04 09:31:38

+1

確實花花公子.......但應用程序在視圖中壓縮DadLoad – user578386 2013-04-04 09:37:06

+0

檢查上面的代碼 – user578386 2013-04-04 09:39:37

回答

-2

無需實現完整的數據源,則可以在一個時間每個用戶按下下一個或後退按鈕時設置與一個視圖控制器PageViewController,像

[pageViewController setViewControllers:@[contentViewController] 
          direction:UIPageViewControllerNavigationDirectionForward 
           animated:YES 
          completion:nil]; 

此作爲切換將動畫頁面轉換。

1

Ah..Finally得到了同樣的問題解決方案..,它可以幫助你..

當我們設定的脊柱位置UIPageViewControllerSpineLocationMid,在pageViewController的doubleSided屬性被自動設置爲YES。這意味着頁面上的內容不會部分顯示在後面。但是,當此屬性設置爲NO時,頁面上的內容將部分顯示在後面,從而使頁面具有半透明效果。因此,在縱向方向上,我們必須將值設置爲NO,否則會導致異常。

UIPageviewcontroller委託方法

因此,在其他部分本doubleSided屬性添加的是,當您返回spineLocation爲UIPageViewControllerSpineLocationMid

self.pageViewController.doubleSided = YES; 
return UIPageViewControllerSpineLocationMid; 
19

問題是你傳遞包含兩個視圖控制器到頁面視圖控制器,而陣列它預計,一次一個,改變你的陣列是這樣的:

NSArray *viewControllers = @[appDelegate.firstViewController]; 

你會通過的意見之一,但viewControllerAfterViewControllerviewControllerBeforeViewController將處理其餘的。

+6

爲什麼這個調用的參數是一個數組,如果它只需要一個視圖控制器? – Shoerob 2015-09-01 03:01:32

+6

@Shoerob根據文檔'對於過渡樣式'UIPageViewControllerTransitionStylePageCurl',如果'doubleSided'是'YES'且主幹位置不是'UIPageViewControllerSpineLocationMid',則必須包含兩個視圖控制器,因爲後一個視圖控制器被用作背面.' – 2015-11-18 10:59:35

0
self.pageViewController.doubleSided = NO; 
return UIPageViewControllerSpineLocationMid; 

這是異常的解決方案。

在xcode它自我,你可以找到它。

轉到UIPageViewcontroller類那裏你可以看到解釋此類似:

@property (nonatomic, readonly) UIPageViewControllerSpineLocation spineLocation; // If transition style is 'UIPageViewControllerTransitionStylePageCurl', default is 'UIPageViewControllerSpineLocationMin', otherwise 'UIPageViewControllerSpineLocationNone'. 

// Whether client content appears on both sides of each page. If 'NO', content on page front will partially show through back. 
// If 'UIPageViewControllerSpineLocationMid' is set, 'doubleSided' is set to 'YES'. Setting 'NO' when spine location is mid results in an exception. 
@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'. 
相關問題