在navigationController中有一个通用栏按钮项

Rez*_*_Rg 4 uinavigationbar uinavigationcontroller uibarbuttonitem uinavigationitem ios

我已经UINavigationController在我的应用程序中,我希望有权UIBarButtonItem在我的应用程序中显示的所有导航栏中显示.这个按钮会加载菜单,所以我不想手动在每个导航栏中添加这个按钮,因为该功能正在加载菜单,我不想复制/过去此按钮的操作.

有没有什么办法来处理这ViewController.h.m

所以按钮充当通用栏按钮项?

dat*_*inc 12

您可以做的是子类化导航控制器.这是一个例子

@interface NavigationController : UINavigationController
@end

@interface NavigationController () <UINavigationBarDelegate>
@end

@implementation NavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (UIViewController* viewController in self.viewControllers){
        // You need to do this because the push is not called if you created this controller as part of the storyboard
        [self addButton:viewController.navigationItem];         
    }
}

-(void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self addButton:viewController.navigationItem];
    [super pushViewController:viewController animated:animated];
}

-(void) addButton:(UINavigationItem *)item{
    if (item.rightBarButtonItem == nil){
        item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(action:)];
    }
}

-(void) action:(UIBarButtonItem*) button{

}

@end
Run Code Online (Sandbox Code Playgroud)