导航栏未显示当前页码。
它显示 8 个中的 0 个。而它应该显示 8 个中的 1 个。
- (void)viewDidLoad {
[super viewDidLoad];
//modelArray holds the page numbers
modelArray = [[NSMutableArray alloc] init];
for (int index = 1; index <= totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:@"%i", index]];
}
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45)];
_navBar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0];
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:[NSString stringWithFormat:
@"Page %i of %i",
index,
CGPDFDocumentGetNumberOfPages(PDFDocument)]];
[_navBar pushNavigationItem:title animated:NO];
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
contentViewController.page = [modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[thePageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:thePageViewController];
[self.view addSubview:thePageViewController.view];
thePageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[thePageViewController didMoveToParentViewController:self];
//self.view.backgroundColor = [UIColor underPageBackgroundColor];
[self.view addSubview:_navBar];
}
尝试使用 currentIndex 和 pageNumber 而不是仅使用索引。仅使用索引,它显示 46297464 of 8,当我使用 currentIndex 或 pageNumber 时,它显示 0 of 8。而它应该显示 1 of 8。
这就是我使用 pageviewcontroller 数据源方法的方式
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == 0) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex - 1];
return contentViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
//get the current page
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == totalPages - 1) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex + 1];
return contentViewController;
}
感谢帮助。