几天来,我一直在尝试使用 UIPageViewController 来显示 3 个不同的笔尖,并且几乎可以正常工作。我仍然有一个我无法弄清楚的奇怪错误。
基本上应用程序启动,我可以一个接一个地在 3 个页面之间滚动,没有任何问题,例如:
第1页->第2页->第3页
然后回到开始:
第3页->第2页->第1页。
没问题。问题是,如果我滚动,例如从Page3->Page2滚动,然后返回到Page3, Page3 在卡入到位时消失。如果我滚动到第四页所在的位置,那么我会得到 Page3。这是与 UIPageViewController、nib 和 UIPageViewController 的委托方法相关的代码:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
indexTest = 0;
Page1 *p1 = [[Page1 alloc]initWithNibName:@"Page1" bundle:nil];
p1.view.tag = 1;
Page2 *p2 = [[Page2 alloc]initWithNibName:@"Page2" bundle:nil];
p2.view.tag = 2;
Page3 *p3 = [[Page3 alloc]initWithNibName:@"Page3" bundle:nil];
p3.view.tag = 3;
NSArray *arr = [[NSArray alloc] initWithObjects:p1,nil];
viewControllers = [[NSMutableArray alloc] initWithObjects:p1,p2,p3, nil];
[self.pageViewController setViewControllers:arr direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.dataSource = self;
[self addChildViewController:self.pageViewController];
[[self view] addSubview:[self.pageViewController view]];
[self.pageViewController didMoveToParentViewController:self];
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
#pragma mark - page view controller stuff
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (indexTest > 0) {
switch (indexTest) {
case 1:{
NSLog(@"NO page is BEFORE current page");
break;
}
case 2:{
NSLog(@"Page BEFORE is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:0] ] );
indexTest--;
return [viewControllers objectAtIndex:0];
break;
}
default:{
NSLog(@"PROBLEM in viewBEFORE, indexTest = %d!!!!", indexTest);
break;
}
}
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (indexTest < NUM_OF_PAGES) {
switch (indexTest) {
case 0:{
NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:1] ] );
indexTest++;
return [viewControllers objectAtIndex:1];
break;
}
case 1:{
NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:2] ] );
indexTest++;
return [viewControllers objectAtIndex:2];
break;
}
case 2:{
NSLog(@"No pages AFTER this current page %d", indexTest);
break;
}
default:{
NSLog(@"PROBLEM in viewAFTER, indexTest = %d!!!!", indexTest);
break;
}
}
}
return nil;
}
最后是页面索引点代码
#pragma mark - dot controller
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
// The number of items reflected in the page indicator.
return NUM_OF_PAGES;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return 0;
}
非常感谢任何和所有的帮助,我想我只是在做一些我看不到的愚蠢的事情,因为我已经接近它完全工作了。如果有什么不清楚或我没有提供足够的信息,请告诉我,我会尽我所能回答。
谢谢