2012-04-13 34 views
6

Tôi có UIViewController chứa các ViewControllers khác. Ban đầu ViewController được thiết lập trong viewDidLoad:Xoay của UIPageViewController trong Bộ điều khiển Xem Vùng chứa

FirstViewController *first = [FirstViewController alloc] init]; 
first.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
first.view.frame = m_content.frame; 

[self addChildViewController:first]; 
[m_content.view addSubview:first.view]; 
[first didMoveToParentViewController:self]; 
m_activeViewController = first; 

này điều khiển container đã thực hiện automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers trở YES. Nó cũng đã thực hiện để manualy thay đổi luân chuyển về phía trước để ViewControllers không hoạt động

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    for(UIViewController *vc in m_viewControllers) 
    { 
     if(vc != [m_activeViewController] 
      [vc willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    } 
} 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    for(UIViewController *vc in m_viewControllers) 
    { 
     if(vc != [m_activeViewController] 
      [vc didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
    } 
} 

Khi nút menu được khai thác để tôi làm cho sự chuyển tiếp giữa ViewControllers.

- (void)onMenuItemTapped:(id)sender 
{ 
    UIViewController *vc = [m_viewControllers objectAtIndex:sender.tag]; 

    vc.view.frame = m_content.frame; 
    [self addChildViewController:vc]; 
    [self transitionFromViewController:m_activeViewController toViewController:vc duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) { 
    [vc didMoveToParentViewController:self]; 
    [m_activeViewController removeFromParentViewController]; 
    m_activeViewController = vc; 
    }]; 
} 

Quá trình chuyển đổi này hoạt động bình thường sau khi thay đổi định hướng, ngay cả khi chúng không hoạt động. Tuy nhiên, một trong những bộ điều khiển xem trẻ em này, được gọi là SecondCV, có UIPageViewController làm Bộ điều khiển xem trẻ em. Tôi có UIPageViewControllerDelegateUIPageViewControllerDataSource thiết lập để SecondCV này và trong pageViewController: spineLocationForInterfaceOrientation: tôi trở UIPageViewControllerSpineLocationMin cho Portrait và UIPageViewControllerSpineLocationMid cho Cảnh. Xoay cho SecondVC này hoạt động chính xác khi nó hoạt động - có hai trang trên cảnh quan và một trang trên chế độ dọc được hiển thị chính xác. Nhưng khi SecondVC này không hoạt động thì vòng quay không chính xác. Ngay cả khi pageViewController: spineLocationForInterfaceOrientation: được gọi, vẫn còn một trang ở cả chế độ Chân dung và Phong cảnh. Tôi đang cố gắng sửa lỗi này một thời gian, nhưng tôi không thấy bất kỳ tùy chọn nào khác. Bạn có ý tưởng nào để sửa lỗi này không?

Cảm ơn bạn.

Trả lời

1

Tôi đã giải quyết loại khó khăn này.

Thực ra SecondVC đã được xoay chính xác hoặc ít nhất, vị trí cột sống đã được thay đổi chính xác. Vì vậy, khi tôi đã xoay thiết bị và SecondVC không phải là bộ điều khiển chế độ xem đang kích hoạt trong chế độ xem chính, thì HAS đã nhận được thông báo xoay và pageViewController: spineLocationForInterfaceOrientation: cũng được gọi. Tôi đặt đúng vị trí cột sống cho định hướng giao diện mới và được gọi là setViewControllers đến bộ điều khiển xem trang của SecondVC. Vấn đề là, ngay cả khi tôi đặt hai bộ điều khiển xem cho UIPageViewControllerSpineLocationMid, sau khi hiển thị SecondVC vẫn chỉ là một, như trong UIPageViewControllerSpineLocationMin.

Tôi đã "cố định" vấn đề, bằng cách thiết lập điểm kiểm soát để xem trang điều khiển trong viewWillAppear: một lần nữa, bởi vì khi phương pháp này được gọi, vị trí cột sống để xem trang điều khiển là chính xác và dựa trên thông tin này tôi đặt một , hoặc hai trang (xem bộ điều khiển)

- (void)viewWillAppear:(BOOL)animated 
{ 
if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMid) 
{ 
    LeftPageController *left; 
    RightPageController *right; 
    if(m_pageController.viewControllers.count > 0) 
    { 
     left = [m_pageController.viewControllers objectAtIndex:0]; 

     right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"]; 
     [right loadView]; 
    } 

    [m_pageController setViewControllers:[NSArray arrayWithObjects:left, right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil]; 

    [m_left hideGallery]; 
} 

if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMin) 
{ 

    [m_pageController setViewControllers:[NSArray arrayWithObject:[m_pageController.viewControllers objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil]; 
} 

[super viewWillAppear:animated]; 
} 


- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
if(UIInterfaceOrientationIsPortrait(orientation)) 
{ 
    LeftPageController *left = [pageViewController.viewControllers objectAtIndex:0]; 
    [pageViewController setViewControllers:[NSArray arrayWithObject:left] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil]; 

    pageViewController.doubleSided = NO;  

    return UIPageViewControllerSpineLocationMin; 
} 

if(UIInterfaceOrientationIsLandscape(orientation)) 
{  
    LeftPageController *left; 
    RightPageController *right; 
    if(pageViewController.viewControllers.count > 0) 
    { 
     left = [pageViewController.viewControllers objectAtIndex:0]; 
     right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"]; 
     [right loadView]; 
    } 

    [pageViewController setViewControllers:[NSArray arrayWithObjects:left,right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil]; 

    return UIPageViewControllerSpineLocationMid; 
} 

return UIPageViewControllerSpineLocationMin; 
} 

Tôi biết tôi đang làm điều tương tự hai lần, nhưng đó là giá tôi sẵn sàng trả trong trường hợp này. Tại sao sau khi gọi pageViewController: spineLocationForInterfaceOrientation: và đặt hai bộ điều khiển chế độ xem cho Cảnh, khi SecondVC không hoạt động, chỉ có một ViewController trong m_pageController trong viewWillAppear: sau khi kích hoạt SecondVC vẫn là một bí ẩn đối với tôi.

+0

Tôi đang sử dụng bộ điều khiển xem trang, và tôi đã thêm VC vào một mảng, nhưng không biết cách thích hợp để chuyển đổi các VC khác nhau, làm cách nào tôi có thể chuyển sang trang tiếp theo. Bạn có thể vui lòng hướng dẫn tôi cho điều đó hoặc đăng một số mã mẫu của nó. –

+0

[link] (https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html) Tôi nghĩ bạn có thể tìm thấy mọi thứ ở đây, nếu không, hãy liên hệ với tôi – DaNY

+0

Tôi chưa có giải pháp nào, xin hãy xem câu hỏi của tôi nếu bạn có thể hướng dẫn tôi. "http://stackoverflow.com/questions/13776894/uipageviewcontroller-change-from-two-page-mode-to-one-page-mode-without-changi#comment18942540_13776894" –

0

tôi bao gồm mã sau đây để giải quyết vấn đề này trong iOS 6:

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 

    NSArray *allPageControllers = pageViewController.viewControllers 

    if (allPageControllers != nil) { 
     [self setViewControllers:allPageControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
    } 

    else { 

     UIViewController *defaultViewController = [[UIViewController alloc] init]; 
     [self setViewControllers:[NSArray arrayWithObject:defaultViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
     [defaultViewController release]; 
    } 

    return UIPageViewControllerSpineLocationMin; 
} 

Hy vọng nó sẽ giúp người!

Các vấn đề liên quan