有没有办法以编程方式告知某个页面在pageviewcontroller中显示的时间?例如,我将其实例化为以下内容:
if (index == 0) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OnCampusTable"];
}
else if (index == 1) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OffCampusTable"];
}
else if (index == 2) {
pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyEventsTable"];
}
我想写点什么
if (current page is "OnCampusTable") {
method 1
}
else if (current page is "OffCampusTable") {
method 2
}
else if (current page is "MyEventsTable") {
method 3
}
我已尝试使用索引,但由于页面加载和填充的方式,它实际上无法正常工作。我正在考虑尝试访问页面指示符(小圆圈的东西)来获取页码,但我不知道该怎么做。
答案 0 :(得分:0)
您可以使用委托方法pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
来跟踪当前页面,然后根据页面执行某些方法。
答案 1 :(得分:0)
答案 2 :(得分:0)
我已经看到一些使用ViewControllers数组并进行比较的解决方案,但我不想保留一堆控制器(我从我的模型中动态生成它们),所以我发现似乎有效的解决方案。
基本上我将属性指向方法之前和之后的数据源中的前一个和下一个控制器。在委托的didFinishAnimating调用中,我将新控制器与这些属性进行比较,看看我们是向前还是向后移动。需要注意的是,如果控制器已经加载,则不会调用前后,所以我根据didFinishAnimating的previousViewController的值重新分配我的属性。
注意,我只针对显示一页的情况实现了这一点。
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (self.pageIndex > 0) {
UIViewController *vc = [self genController:self.trackList[self.pageIndex - 1]];
self.priorVC = vc;
return vc;
} else {
self.priorVC = nil;
return nil;
}
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (self.pageIndex < self.trackList.count - 1) {
UIViewController *vc = [self genController:self.trackList[self.pageIndex + 1]];
self.nextVC = vc;
return vc;
} else {
self.nextVC = nil;
return nil;
}
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if (finished && completed) {
if (pageViewController.viewControllers.lastObject == self.priorVC) {
NSLog(@"Back");
self.pageIndex--;
self.nextVC = previousViewControllers.lastObject;
} else if (pageViewController.viewControllers.lastObject == self.nextVC) {
NSLog(@"Forward");
self.pageIndex++;
self.priorVC = previousViewControllers.lastObject;
}
NSLog(@"Page: %ld",self.pageIndex);
}
}