2013-04-04 26 views
27

Tôi đang sử dụng UIPageViewController trên iPad, nơi tôi cần hiển thị firstviewController ở trang đầu tiên và ContentViewController trong trang tiếp theo trong khổ ngang.cách thêm hai bộ điều khiển chế độ xem trong UIPageViewcontroller

Nếu tôi đặt NSArray với hai viewControllers ứng dụng là treo tại [self.pagviewController setViewController:] với ngoại lệ sau:

Số lượng điều khiển xem được cung cấp (2) không khớp với số lượng yêu cầu (1) cho yêu cầu vị trí cột sống (UIPageViewControllerSpineLocationMin)

Dưới đây là các mã:

#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

Bạn cần cung cấp một trang tại một thời điểm thông qua nguồn dữ liệu của bạn nếu bạn chỉ muốn hiển thị một trang, đó là lý do tại sao ứng dụng của bạn sự cố - nó có hai trang để hiển thị khi trang mong đợi (khi ở chế độ dọc tôi đoán). Hiển thị cho chúng tôi mã đại biểu và nguồn dữ liệu của bạn. –

+1

chắc chắn dude ....... nhưng creashes ứng dụng trong viewDidLoad – user578386

+0

kiểm tra mã trên – user578386

Trả lời

-2

Thay vì thực hiện một nguồn dữ liệu đầy đủ, bạn có thể thiết lập PageViewController với một bộ điều khiển chế độ xem một mỗi khi người dùng đẩy một nút bên cạnh hoặc phía sau, giống như

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

này sẽ animate chuyển trang như bạn chuyển đổi.

1

Ah..Finally có giải pháp cho vấn đề này cùng .., nó có thể giúp bạn ..

Khi chúng tôi thiết lập vị trí cột sống để UIPageViewControllerSpineLocationMid, các doubleSided tài sản của pageViewController được tự động thiết lập để YES. Điều này có nghĩa là nội dung ở mặt trước trang sẽ không hiển thị một phần qua lại. Nhưng khi thuộc tính này được đặt thành NO, nội dung ở mặt trước trang sẽ hiển thị một phần qua lại, tạo cho trang một loại hiệu ứng mờ. Vì vậy, theo hướng dọc, chúng ta phải đặt giá trị thành NO, nếu không nó sẽ dẫn đến một ngoại lệ.

Vì vậy, trong phương pháp đại biểu UIPageviewcontroller của bạn, một phần khác thêm tài sản doubleSided này như YES khi bạn quay trở lại spineLocation như UIPageViewControllerSpineLocationMid

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

Vấn đề là bạn đi qua một mảng chứa hai bộ điều khiển nhằm xem trang điều khiển trong khi nó hy vọng một lúc, thay đổi mảng của bạn là như thế này:

NSArray *viewControllers = @[appDelegate.firstViewController]; 

bạn sẽ vượt qua một trong những điểm nhưng viewControllerAfterViewControllerviewControllerBeforeViewController sẽ xử lý phần còn lại.

+6

Tại sao tham số cho cuộc gọi này là một mảng nếu nó chỉ mong đợi một bộ điều khiển xem? – Shoerob

+6

@Shoerob Theo tài liệu 'Để chuyển đổi kiểu 'UIPageViewControllerTransitionStylePageCurl', nếu 'doubleSided' là 'YES' và vị trí cột sống không phải là 'UIPageViewControllerSpineLocationMid', hai bộ điều khiển xem phải được bao gồm, vì bộ điều khiển xem sau được sử dụng như mặt sau .' –

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

Đây là giải pháp cho ngoại lệ.

Trong xcode nó tự bạn có thể tìm thấy điều này.

Đến lớp UIPageViewcontroller đó bạn có thể thấy lời giải thích cho điều này như:

@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'. 
Các vấn đề liên quan