Navigation Controller - Adds a custom navbar item to all views

I am creating a navigation controller in my appDelegate (programmatically). I would like to add a custom button to the navigation bar so that it shows all 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)?

+3


source to share


1 answer


You can create a simple hierarchy for your ViewControllers:

                               UIViewController
                                       |
                      CustomBarButtonItemViewController
                       /               |             \
                      /       SecondViewController    \
           FirstViewController                 ThirdViewController

      

CustomBarButtonItemViewController

will overwrite -viewDidLoad

, for example:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIbarButtonItem *barButtonItem = ...;
    self.navigationItem.rightBarButtonItem = barButtonItem;
}

      



Then create First-

, Second-

and ThirdViewController

how to subclass CustomBarButtonItemViewController

:

@interface FirstViewController : CustomBarButtonItemViewController

@interface SecondViewController : CustomBarButtonItemViewController

@interface WhateverYouLikeViewController : CustomBarButtonItemViewController

      

Make sure you call [super viewDidLoad]

in your third level subclasses!

+9


source







All Articles