BackBarButtonItem 在其上显示导航栏标题,并在每次翻页时更新

Posted

技术标签:

【中文标题】BackBarButtonItem 在其上显示导航栏标题,并在每次翻页时更新【英文标题】:BackBarButtonItem shows navbar title on it and updates each time when page is turned over 【发布时间】:2013-07-06 15:04:58 【问题描述】:

在 ViewController 的第一次推送时,它会显示导航栏标题和 backbarbuttonitem,但是当我翻页时,导航栏标题会更新,这符合要求,但同时导航栏标题会反映在 backbarbuttonitem 上,并且每次我转身时都会更新翻页

执行 PDF Segue。这是我的参考代码

- (IBAction)ReadAction:(id)sender 

[self performSegueWithIdentifier:@"MySegue" sender:sender];

 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 
if ([[segue identifier] isEqualToString:@"MySegue"]) 

    // Get destination view
    PDFViewController *pdfviewController = [segue destinationViewController];

    // Get button tag
    NSInteger tagIndex = [(UIButton *)sender tag];

    // Set the selected button in the new view
    [pdfviewController setSelectedButton:tagIndex];


这是PDFViewController显示PDF文件的实现文件

#import "PDFViewController.h"
#import "PageViewController.h"
@implementation PDFViewController
 @synthesize selectedButton;

- (void)viewDidLoad

[super viewDidLoad];

NSString *path = [[NSBundle mainBundle] pathForResource:@"papertheme" ofType:@"pdf"];
PageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path];
[self presentViewController:page animated:NO completion:NULL];
// Do any additional setup after loading the view, typically from a nib.            
 

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

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

  [_navBar pushNavigationItem:contentViewController.navigationItem animated:NO];

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

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

  [_navBar pushNavigationItem:contentViewController.navigationItem animated:NO];

return contentViewController;

 


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



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

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

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

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonBackPressed:)];

  [_navBar pushNavigationItem:contentViewController.navigationItem animated:NO];
  contentViewController.navigationItem.leftBarButtonItem = backBarButtonItem;

_toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 960, self.view.bounds.size.width, 45)];

_toolbar.barStyle = UIBarStyleBlackOpaque;

_toolbar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0];

// Toolbar Items
_previousButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(gotoPreviousPage)];
_nextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(gotoNextPage)];
_actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];

// Toolbar items & navigation
UIBarButtonItem *fixedLeftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
fixedLeftSpace.width = 32; // To balance action button
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSMutableArray *items = [[NSMutableArray alloc] init];
if (_displayActionButton) [items addObject:fixedLeftSpace];
[items addObject:flexSpace];
if (CGPDFDocumentGetNumberOfPages(PDFDocument) > 1) [items addObject:_previousButton];
[items addObject:flexSpace];
if (CGPDFDocumentGetNumberOfPages(PDFDocument) > 1) [items addObject:_nextButton];
[items addObject:flexSpace];
if (CGPDFDocumentGetNumberOfPages(PDFDocument) > 1) [items addObject:_actionButton];

    [_toolbar setItems:items];

 [self.view addSubview:_toolbar];

[self.view addSubview:_navBar];
 

知道如何禁用 backbarbuttonitem 显示导航栏标题以及在我翻页时进行更新。

感谢帮助。

非常感谢。

【问题讨论】:

【参考方案1】:

使用您想要的标题和操作实例化一个 UIBarButtonItem 并将其分配给 backBarButtonItem 属性

@implementation PageViewController

    - (void)viewDidLoad
    
        [super viewDidLoad];
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                                  style:UIBarButtonItemStyleBordered target:self
                                                                                 action:@selector(goBack:)];
    

    - (void)goBack:(id)sender;
    
        //logic to go back
    

【讨论】:

感谢您的回复。欣赏它,但仍然是相同的输出

以上是关于BackBarButtonItem 在其上显示导航栏标题,并在每次翻页时更新的主要内容,如果未能解决你的问题,请参考以下文章

悬停图像 - 在其上显示 div

QLPreviewController 在其上显示模态视图控制器后失去联系

导航栏返回按钮

如何使用不同颜色快速设置导航栏返回箭头和backBarButtonItem标题

活动(主题对话框)在其上的来电屏幕阻塞

将图像和标题都添加到导航 backBarButtonItem