更新导航栏

时间:2013-07-04 15:37:50

标签: iphone ios objective-c uinavigationbar uipageviewcontroller

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

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

请帮忙。

欣赏它。

感谢。

1 个答案:

答案 0 :(得分:2)

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

 _navBar.topItem.title = @"Custom title";
[_navBar pushNavigationItem:_navBar.topItem animated:NO];