2013-03-07 73 views
0

我使用xcode中的基於頁面的應用程序模板創建一個RootViewController,一個DataViewController和一個ModelController。從DataViewController轉到UIPageViewController的頁面?

我已經設法讓它進入我需要的任何頁面,從RootViewController,但我也需要從DataViewController中觸發一個來自特定頁面的事件。這不太好。我應該如何改變它的代碼,使其從DataViewController工作?

我的代碼如下所示: - 從根(工作):

//get current index of current page 
DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 
NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController]; 

//get the page(s) to go to 
DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(pageToGoTo - 1) storyboard:self.storyboard]; 

//put it(or them if in landscape view) in an array 
NSArray *theViewControllers = nil; 
theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil]; 

//check which direction to animate page turn then turn page accordingly 
if (retreivedIndex < (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){ 
[self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 
} 
if (retreivedIndex > (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){ 
[self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL]; 
} 

- 從一個頁面,dataviewcontroller(不工作):

DataViewController *targetPageViewController = [((UIPageViewController*)self.parentViewController) viewControllerAtIndex:(pageToGoTo - 1) storyboard:self.storyboard]; //The problem is here, but I do not know how to solve it. 


//put it(or them if in landscape view) in an array 
NSArray *theViewControllers = nil; 
theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil]; 


[((UIPageViewController*)self.parentViewController) setViewControllers: theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 

回答

1

我這裏 https://stackoverflow.com/a/16435166/1463518回答了這個問題

我這樣做的方式是通過使用通知

我在viewDidLoad中添加按鈕單擊通知在DataViewController

[[NSNotificationCenter defaultCenter]postNotificationName:@"H2RAutoPage" object:nil]; 

而在RootViewController的

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(autoPage:) name:@"H2RAutoPage" object:nil]; 

我自動創建程序看起來像這樣

- (void)autoPage: (NSNotification *)notification 
{ 
//get current index of current page 
    NSUInteger currentPage = [self.modelController indexOfViewController:[self.pageViewController.viewControllers objectAtIndex:0]]; 
// this is the next page nil mean we have reach the end 
    H2RDataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(currentPage + 1) storyboard:self.storyboard]; 
    if (targetPageViewController) { 
     //put it(or them if in landscape view) in an array 
     NSArray *viewControllers = [NSArray arrayWithObjects:targetPageViewController, nil]; 
     //add page view 
     [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 
    } 

} 

我所做的還嘗試使用委託,但發現代碼變得混亂,因爲它不斷失去委託我發現NSnotification更乾淨。

+0

謝謝,我結束了使用委託。不知道在撰寫本文時是什麼。我想這個解決方案也可以工作。 – 2013-05-21 13:19:36

相關問題