2013-01-07 23 views
3

我想了解蘋果的基於頁面的應用程序的默認模板。該模板顯示了一年的每個月都有一頁的日曆。UIPageViewController - 第一次旋轉重設視圖到初始頁面?

編譯模板時沒有任何改變,我注意到一些奇怪的東西。當應用程序啓動時,它將以縱向模式顯示,以及何時 - 在翻閱某些頁面(例如,到六月)之後 - 更改旋轉,它會以橫向模式重新加載,顯示兩頁,但開始時間爲一月。雖然這隻會發生一次。在隨後的方向更改時,它會跳轉到正確的「currentViewController」。

該代碼,這似乎來自RootViewController的文件,特別是

UIViewController *currentViewController = self.pageViewController.viewControllers[0]; 

坦率地說,這似乎爲第一取向的變化被忽略。

我想明白爲什麼?

+1

有趣!這隻發生在iOS6中。在5.1模擬器中運行時,完全相同的代碼正確執行。我已經設置了一個斷點,並注意到在5.1中,當UIPageviewcontrol第一次加載時,不會執行函數「 - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation',但是在6.0中是這樣的。 – user1459524

+0

檢查了這一點 http://stackoverflow.com/questions/14132132/uipageviewcontroller-resets-to-first-page-on-rotation – Oscar

+1

謝謝,我已經看到這個線程之前,我張貼,但這是無關的我發佈的內容我認爲。我使用Apple提供的基於默認頁面的模板,沒有任何改變,旋轉錯誤只發生在第一次方向改變(並且只在iOS6中)。它適用於後續的方向更改。 – user1459524

回答

5

更新應用程序時,我不得不面對同樣的問題,以使iPhone 5更友好。使用iOS 6 sdk進行編譯時,問題出現在iOS 6上,但不出現在iOS 5上。僅當從橫向進入PageViewController時纔會發生此問題。

一種解決方法:

創建一個屬性來保存您的m的@interface部分的當前狀態:在willRotateToInterfaceOrientation

@property (strong, nonatomic) NSArray *viewControllersSave; 

保存viewControllers:

(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    // some code here... 
    self.viewControllersSave = self.pageViewController.viewControllers; 
} 

使用保存的屬性而不是spineLocationForInterfaceOrientation中的PageViewController屬性。

- (UIPageViewControllerSpineLocation)pageViewController: (UIPageViewController *)pageViewController 
        spineLocationForInterfaceOrientation: (UIInterfaceOrientation)orientation 
{ 
    // some code 
    NSArray *viewControllers = @[self.viewControllersSave[0]]; 
    [self.pageViewController setViewControllers:viewControllers 
          direction:UIPageViewControllerNavigationDirectionForward 
          animated:YES 
          completion:NULL]; 

return UIPageViewControllerSpineLocationMin; 
} 

這可能是爲了避免該問題的可能性,但你應該提交給蘋果一個bug報告,以避免這樣的解決辦法以備後用。

+0

謝謝!它並沒有爲我開箱,因爲我一直在獲取SIGABRT,直到我將代碼從'willRotateToInterfaceOrientation'移動到'shouldAutorotateToInterfaceOrientation'。我也稍微改變了它,而不是保存到一個數組,我聲明'@property(強,非原子)UIViewController * viewControllerSave;'並保存'self.viewControllerSave = self.pageViewController.viewControllers [0];'這有益於在橫向和縱向上使用我的'spineLocationForInterfaceOrientation'代碼。我接受你的回答,因爲它指向了我正確的方向! – user1459524

+0

我的意思當然是'supportedInterfaceOrientations'。 – user1459524

+0

感謝您的接受,您不應執行任何其他代碼,而是計算supportedInterfaceOrientations中支持的方向的代碼。 – dulgan