【发布时间】:2015-10-01 08:17:26
【问题描述】:
我正在实现一个基于页面的应用程序。
基本上 pageViewController 将用于显示数据页面。
相同的格式,不同的数据。
创建了一个 ViewController 来保存 pageViewController 和另一个将呈现数据的 viewController。
我只需要一个不同的 viewController 来处理数据,我发现这可能是个问题。
普通的分页器,如 Android ViewPager,会要求您返回给定索引的想要显示的视图,PageViewController 不能这样工作,它只会要求您提供下一个和上一个视图控制器。
我希望将索引存储在内容 ViewController 的属性中可以完成这项工作……但事实并非如此。
它正在部分工作,但是..
这是我的相关代码:
- (void)viewDidLoad {
[super viewDidLoad];
page = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[page setDelegate:self];
[page setDataSource:self];
index = 0;
CampaignContent * content = [[CampaignContent alloc]initWithNibName:@"CampaignContent" bundle:nil];
[content setTitlestr:@"Index 0"];
[content setDesc:@"desc1"];
[content.view setTag:1];
[page setViewControllers:@[content] direction:UIPageViewControllerNavigationDirectionForward animated:TRUE completion:nil];
[page willMoveToParentViewController:self];
[self addChildViewController:page];
[self.view addSubview:page.view];
[page didMoveToParentViewController:self];
}
-(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
last=false;
NSLog(@"move right index %i",index);
CampaignContent * content = [[CampaignContent alloc]initWithNibName:@"CampaignContent" bundle:nil];
[content setTitlestr:[NSString stringWithFormat:@"Index %i",index+1]];
[content.view setTag:viewController.view.tag+1];
return content;
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if(completed)
{
if([[previousViewControllers objectAtIndex:0] view].tag<index){
NSLog(@"Left");
index = index-1;
}else{
NSLog(@"Right");
index = index+1;
}
NSLog(@"didFinishAnimating view tag %i",[[previousViewControllers objectAtIndex:0] view].tag,index);
}
}
-(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
last= true;
NSLog(@"move left index %i",index);
CampaignContent * content = [[CampaignContent alloc]initWithNibName:@"CampaignContent" bundle:nil];
[content setTitlestr:[NSString stringWithFormat:@"Index %i",index-1]];
[content.view setTag:viewController.view.tag-1];
return content;
}
只是想看到索引正常工作,但...
从 0 开始,我滑动到下一页,我在日志中看到以下内容
move right index 0
move left index 0
Right
didFinishAnimating view tag 1
move right index 1
哇,这么多电话...但索引显示仍然正常
再次滑动到下一页
Right
didFinishAnimating view tag 2
move right index 2
至此索引显示正确,从改变方向开始问题。
滑动到上一个现在我看到了:
Right
didFinishAnimating view tag 3
move left index 3
在确定滑动方向时,didFinishAnimating 似乎失败,但显示的索引仍然正常
再次向左滑动
Left
didFinishAnimating view tag 2
这里似乎 viewControllerBeforeViewController 没有被调用,我们已经从 0->1->2->1->0 走了,对吧?但显示的索引已经完成 0->1->2->1->2
如果我再次向左滑动,我可以看到它转到 ->1->0->-1,就像它应该的那样。
同样适用于其他方式。
所以,我认为 viewControllerBeforeViewController 和 viewControllerAfterViewController 不可靠,didFinishAnimating 无法单独确定滑动方向。
有什么方法可以让我正确地做到这一点,或者我只是在做坏事?
对不起我的大帖子,希望有人能帮助我。
【问题讨论】:
标签: ios objective-c uipageviewcontroller