从DataViewController转到UIPageViewController的页面?

时间:2013-03-07 14:26:17

标签: xcode

我正在使用xcode中基于页面的应用程序模板,它创建了一个RootViewController,一个DataViewController和一个ModelController。

我已经设法从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 个答案:

答案 0 :(得分:1)

我在这里回答了这个问题 https://stackoverflow.com/a/16435166/1463518

我这样做的方法是使用通知

我在DataViewController

中点击按钮添加了通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"H2RAutoPage" object:nil];

在viewDidLoad的

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

我的AutoPage Routine看起来像这样

- (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更清洁。