iOS在整个导航控制器中显示相同的UIBarButton

时间:2013-11-17 06:09:06

标签: ios cocoa-touch uinavigationcontroller uibarbuttonitem

目前我想创建一些按钮并在整个导航控制器中使用它。假设用于搜索的“搜索/聚焦”按钮或“添加项目按钮”。这些应该在所有导航控制器中使用。

我试图通过互联网进行搜索,但仍然无法做到这一点。

2 个答案:

答案 0 :(得分:0)

或尝试使用UINavigationController类别,并自定义后退项,因此无论推送多少次,只需为navigationController的后退项设置相同的视图。类似的东西:

@implementation UIViewController (CustomNavigation)

- (void)setLeftBarButtonItem:(UIButton*)leftview
{    
    UIBarButtonItem *customItem = [[[UIBarButtonItem alloc] initWithCustomView:leftview] autorelease];
    self.navigationItem.leftBarButtonItem = customItem; 
    [(UIButton*)self.navigationItem.leftBarButtonItem.customView addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
}

答案 1 :(得分:0)

我使用BaseViewController作为UIViewController的子类,并继承了BaseViewController类的所有ViewController。 在viewDidLoad中输入代码,将按钮添加到navigationBar。 使用BaseViewController继承的所有ViewControllers都会显示您的按钮。

任何带有navigationController和NavigationBar的UIViewController都有自己的导航按钮,你必须每次都覆盖它。继承你做一次。

@interface BaseViewController : UIViewController
@end

@implementation BaseViewController 
- (void)viewDidLoad {
    [super viewDidLoad];        
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
  }
  @end