我这样做的方法是使用通知
我在 DataViewController 中的按钮单击上添加了一个通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"H2RAutoPage" object:nil];
并在 RootViewController 中的 viewDidLoad
[[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];
}
}
我也尝试过使用委托,但发现代码变得混乱,因为它不断丢失委托,我发现 NSnotification 更干净。