Xcode 是否从DataViewController转到UIPageViewController的页面?
我使用的是xcode中基于页面的应用程序模板,它创建了一个RootViewController、一个DataViewController和一个ModelController 我已经设法从RootViewController转到我需要的任何页面,但我也需要从DataViewController转到这个页面,在特定页面的事件中触发。这不太管用。我应该如何更改代码以使其从DataViewController工作 我的代码如下所示: -从根目录(工作):Xcode 是否从DataViewController转到UIPageViewController的页面?,xcode,Xcode,我使用的是xcode中基于页面的应用程序模板,它创建了一个RootViewController、一个DataViewController和一个ModelController 我已经设法从RootViewController转到我需要的任何页面,但我也需要从DataViewController转到这个页面,在特定页面的事件中触发。这不太管用。我应该如何更改代码以使其从DataViewController工作 我的代码如下所示: -从根目录(工作): 我在这里回答了这个问题 我这样做的方式是使用通
我在这里回答了这个问题 我这样做的方式是使用通知 我在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];
}
}
我也尝试过使用委托,但发现代码变得凌乱,因为它不断丢失我发现的委托。谢谢,我最终使用了委托。在写这篇文章的时候,我不知道那是什么。我想这个解决方案也能奏效。
[[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];
}
}