Reputation: 124
I am creating a navigation controller in my appDelegate (programmatically). I would like to add a custom button to the nav bar so that it will show up all of the views.
I can get the button to show if I add it in the ViewDidLoad methods of each controller, but is there a way I can add the button only once (i.e. in the appDelegate where I create the nav controller)?
Upvotes: 3
Views: 1431
Reputation: 45210
You can create a simple hierarchy for your ViewControllers:
UIViewController
|
CustomBarButtonItemViewController
/ | \
/ SecondViewController \
FirstViewController ThirdViewController
The CustomBarButtonItemViewController
will overwrite -viewDidLoad
, like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIbarButtonItem *barButtonItem = ...;
self.navigationItem.rightBarButtonItem = barButtonItem;
}
Then create your First-
, Second-
and ThirdViewController
as subclasses of the CustomBarButtonItemViewController
:
@interface FirstViewController : CustomBarButtonItemViewController
@interface SecondViewController : CustomBarButtonItemViewController
@interface WhateverYouLikeViewController : CustomBarButtonItemViewController
Make sure you call [super viewDidLoad]
in your third-level subclasses!
Upvotes: 9