1

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

执行 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 显示导航栏标题以及在我翻页时进行更新。

感谢帮助。

非常感谢。

4

1 回答 1

0

使用所需的标题和操作实例化 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
    }
于 2013-07-06T15:55:32.337 回答