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

时间:2013-07-06 15:04:59

标签: iphone objective-c ios6 uinavigationbar pushviewcontroller

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

enter image description here enter image description here

任何想法如何禁用backbarbuttonitem显示导航栏标题以及每当我翻页时都会更新。

感谢帮助。

非常感谢。

1 个答案:

答案 0 :(得分: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
    }