0

我要做的就是在用户翻页时更新导航栏。下面是我的代码

- (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];

 [self updateNavigationBar];

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];

 [self updateNavigationBar];

return contentViewController;

}

-(void) updateNavigationBar{

if (CGPDFDocumentGetNumberOfPages(PDFDocument) > 1) {
    title = [NSString stringWithFormat:@"Page %u of %u", currentIndex - 1, CGPDFDocumentGetNumberOfPages(PDFDocument)];
} else if ((CGPDFDocumentGetNumberOfPages(PDFDocument) < 1) {


 title = [NSString stringWithFormat:@"Page %u of %u", currentIndex + 1, CGPDFDocumentGetNumberOfPages(PDFDocument)];

}

但似乎它根本不起作用我能够翻页但无法更新导航栏。

这是导航栏的代码

_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];

title = [[UINavigationItem alloc] initWithTitle:[NSString stringWithFormat:
                                                                   @"Page %u of %u",
                                                                   currentIndex,
                                                                  CGPDFDocumentGetNumberOfPages(PDFDocument)]];
 [_navBar pushNavigationItem:title animated:NO];

请帮忙。

欣赏它。

谢谢。

4

1 回答 1

2

更新导航栏是通过更新导航项来完成的:

 _navBar.topItem.title = @"Custom title";
[_navBar pushNavigationItem:_navBar.topItem animated:NO];
于 2013-07-04T15:55:43.253 回答