0

RootViewController、DataViewController、および ModelController を作成する xcode のページ ベースのアプリケーション テンプレートを使用しています。

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];
4

1 に答える 1

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 ルーチンは次のようになります

- (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];
  }

}

私もデリゲートを使用してみましたが、デリゲートを失い続けたため、コードが乱雑になっていることがわかりました。

于 2013-05-08T07:56:26.007 に答える