iphone - 更新导航栏

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

请帮忙。

欣赏它。

谢谢。

最佳答案

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

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

关于iphone - 更新导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17474063/

相关文章:

ios - 设置页面添加或减去不同的图像阵列。使用 swift 和 xcode 进行编码

objective-c - NSUserDefaults 初始设置

ios - 移动 xcode 项目导致红色文本(项目中缺少文件)

iOS组件可重用性: views and view controllers

objective-c - 加载了 "ChatView"nib 但没有获得 UITableView。

iphone - 使用 pushViewController 时出现 SIGABRT 错误

iPhone:如何知道屏幕上是否有事件的 UIKeyboard?

ios - UIScrollView 以编程方式在 Swift 中

ios - 代码 8 : how to adapt iphone app for iPAD

ios - 在 UIView 角上创建 mask