Objective c 基于分页的iPhone应用程序-使用DataViewController中的RootViewController函数
我正在用故事板和ARC构建一个基于页面的iPhone应用程序。我试图使用RootViewController类中的函数从DataViewController类中的函数内部翻转到特定页面 问题在于DataViewController类的主实例不在RootViewController类中找到,而是在ModelController类中找到。而且ModelController类似乎是只读的(因此我无法从RootViewController类等外部类为ModelController设置委托) 我最初的计划是: 1)将ModelController委托从RootViewController类设置为RootViewController 2)将DataViewController委托设置为ModelController的委托 但这似乎不起作用-我假设是由于ModelController类的只读性Objective c 基于分页的iPhone应用程序-使用DataViewController中的RootViewController函数,objective-c,ios,uipageviewcontroller,Objective C,Ios,Uipageviewcontroller,我正在用故事板和ARC构建一个基于页面的iPhone应用程序。我试图使用RootViewController类中的函数从DataViewController类中的函数内部翻转到特定页面 问题在于DataViewController类的主实例不在RootViewController类中找到,而是在ModelController类中找到。而且ModelController类似乎是只读的(因此我无法从RootViewController类等外部类为ModelController设置委托) 我最初的计划
在这种情况下,如何从DataViewController类使用RootViewController类中的函数?有没有比设置委托更好的方法来完成此任务?我这样做的方法是使用通知 我在DataViewController中的按钮单击上添加了一个通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"H2RAutoPage" object:nil];
在viewDidLoad中的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];
}
}
我也尝试过使用一个委托,但发现代码变得很混乱,因为它不断丢失我发现更干净的委托 只是通过以下操作使其工作:RootViewController*rvc=(RootViewController*)self.parentViewController.parentViewController;[rvc函数TODO];不确定这是否是最好的方法,但目前它还有效