问题
On first Push of ViewController it shows navbar title and backbarbuttonitem but when i turn page over nav bar title gets updated which is good as per requirement but at the same time navbar title reflects on the backbarbuttonitem and gets updated each time when i turn page over
To perform the PDF Segue. Here is my code for reference
- (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];
}
}
This is the implementation file of PDFViewController to display PDF file
#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.
}
In the pageviewcontroller implementation file this is how i m coding to update nav bar title
- (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];
}


Any idea how i can disable backbarbuttonitem from showing nav bar title and also from updating whenever i turn page over.
Appreciate help.
Thanks a lot.
回答1:
Instantiate a UIBarButtonItem with the title and action you want and assign it to the backBarButtonItem property
@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
}
来源:https://stackoverflow.com/questions/17504150/backbarbuttonitem-shows-navbar-title-on-it-and-updates-each-time-when-page-is-tu