问题描述
在第一次按下ViewController时,它会显示导航栏标题和backbarbuttonitem,但是当我翻页导航栏时,标题会根据需要更新,但同时导航栏标题会反映在backbarbuttonitem上,每当我转动时都会更新页面
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
执行PDF Segue。这是我的参考代码
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];
}
}
这是显示PDF文件的PDFViewController的实现文件
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.
}
在pageviewcontroller实现文件中,这是如何编码更新导航栏标题
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];
}
我知道如何禁用backbarbuttonitem显示导航栏标题以及每当我翻页时都会更新。
Any idea how i can disable backbarbuttonitem from showing nav bar title and also from updating whenever i turn page over.
感谢帮助。
非常感谢。
推荐答案
实例化UIBarButtonItem使用您想要的标题和操作并将其分配给属性
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
}
这篇关于BackBarButtonItem在其上显示导航栏标题,并在每次翻页时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!