Search code examples
iphoneobjective-ciosxcodethree20

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


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?


Solution

  • 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