How to add a global right bar button (UINavigationController) in Three20's AppDelegate

我怕爱的太早我们不能终老 提交于 2019-12-19 05:09:05

问题


I want to add a global right bar button in the global AppDelegate so all my view controllers will have this button automatically.

I added in the AppDelegate

navigator.window.rootViewController.navigationController.navigationItem.rightBarButtonItem     
= [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Test", @"") 
style:UIBarButtonItemStyleBordered target:self action:@selector(showTest)] autorelease]; 

Of course the above code is not working..any problem with the code above?


回答1:


Well, I'm not sure you can do it in your way, because UINavigatorController always uses the buttons from the view controller that is currently displayed, and not from the top / root controller.

What you can do is to subclass TTViewController with a new view controller and set your left bar button item.

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation BaseViewController


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIViewController



///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad {
  [super viewDidLoad];


  self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Test", @"") 
                                                                            style:UIBarButtonItemStyleBordered target:self action:@selector(showTest)] autorelease];
}

and then you should extend all your view controllers from this base controller, which contains the right navigation bar item




回答2:


What you need to do is to tap into the navigation controller you wish to hook into. then you can implement UINavigationControllerDelegate (each navigation controller has a delegate property) which will give you these events:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

You can implement

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

and put your right button in place.



来源:https://stackoverflow.com/questions/8388566/how-to-add-a-global-right-bar-button-uinavigationcontroller-in-three20s-appde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!