如何覆盖navigationItem.leftBarButtonItem

时间:2011-11-04 04:59:35

标签: objective-c ios uibarbuttonitem

我在app委托方法'navigationController willShowViewController'方法中设置了我的backbutton。它被添加为显示中的viewcontroller的leftbarButton,如下面的代码所示。

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{

      if (viewController != [navigationController.viewControllers objectAtIndex:0]) {
          CustomNavButton *customBackButton = [[CustomNavButton alloc]initWithBackButtonAndFrame:CGRectMake(0, 0, 52, 33)]; 

          customBackButton.navcon = navigationController;

          UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc]initWithCustomView:customBackButton];
          viewController.navigationItem.leftBarButtonItem = newBackButton;
          [newBackButton release];

          viewController.navigationItem.hidesBackButton = YES;
      }


}

我试图覆盖另一个视图控制器中的左侧按钮,如下所示。但无济于事。

CustomNavButton *feedButton = [[CustomNavButton alloc]initWithTitle:@"Feed" andFrame:CGRectMake(0, 0, 50, 32)];  
[feedButton.customNavButton addTarget:self action:@selector(showFeedPage) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftFeedButton = [[UIBarButtonItem alloc]initWithCustomView:feedButton];

qavc.navigationItem.leftBarButtonItem = leftFeedButton;

似乎appdelegate方法总是在我的viewcontroller中的代码之后运行,因此我的viewcontroller中对左边栏按钮的任何更改都将被app delegate中定义的后退按钮所取代。

到底有没有?

1 个答案:

答案 0 :(得分:1)

这就是我在我的应用程序中所做的,它运行良好:

- (void)addBackAndSave {
    [self.navigationItem setHidesBackButton:YES];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(doSaveAlert:)];
    [self.navigationItem setLeftBarButtonItem:backItem];
    [backItem release];

    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveData:)];
    [self.navigationItem setRightBarButtonItem:saveItem animated:YES];
    [saveItem release];
}