1

I think this should be a simple one but I'm left scratching my head. I have an app with multiple ViewControllers setup in a storyboard. I want each of these to have the same navigation bar at the top of every view but this navigation controller to have the same buttons on all views. The navigation controller is setup in AppDelegate and the buttons are added in the ViewDidLoad of each ViewController (as per an answer I received on here for another question) but at the moment, the buttons (and the title) aren't showing.

Where the UINavigationController is setup - AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

    [self.window setRootViewController:navVC];

    [_window makeKeyAndVisible];

    return YES;
}

One of the Views:

- (void)viewDidLoad
{

    [self setTitle:@"Congress app"];  // < This does not set the title of the NavController

    UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
    UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];

    //self.navigationItem.leftBarButtonItem = showMenuButton;
    //self.navigationItem.rightBarButtonItem = showSettingsButton;
    self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
    self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;

    UINavigationController *currentNav;
    UIViewController *VCname;   
    NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
    NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
    [super viewDidLoad];  
}

I have a couple of NSLogs that show what (if anything) is in the navigation controller, it shows: and which intimates that buttons are being added but not shown?

EDIT:

I have just remembered that there is a ViewController (InitViewController.m) that is fired before the VC in the above code. The NavigationController is allocated here:

- (void)viewDidLoad
{
[super viewDidLoad];

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
}

This is likely to be the cause of the problem.

EDIT 2:

Thanks to Saurav Mac I have tracked the problem down to me trying to add the buttons on the 'wrong' view controller. I had the Navigation Controller setup in AppDelegate.m, the first View Controller is InitViewController which then calls MainViewController to be the 'top view controller'. Can't believe I missed that, thanks for the help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Agamemnon
  • 587
  • 2
  • 15
  • 44
  • Have you removed the 'Main' [your storyboard name] (since you're manually managing the initial controller) on `Project Settings > #target# > Deployment Info > Main Interface` ? – Alladinian Jan 20 '14 at 09:32
  • Dude lets go back to the basics, have you tried to add a plain barbuttonitem first, Instead of trying to add a button an image? Let me know if that works first :) – Pavan Jan 20 '14 at 09:38
  • @Pavan - I've just tried: [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:nil action:@selector(showSettings:)]animated:YES]; still no luck... – Agamemnon Jan 20 '14 at 09:48
  • Please review this migth solve your problem http://stackoverflow.com/a/26269894/4188824 – Manu Gupta Dec 30 '14 at 12:37

5 Answers5

6

Either you can create this button in a common class i.e subclass of NSObject and then can call this method in every class you want to add button or you can add this code to ViewDidLoad method of every class -

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];

UIImage *img1=[UIImage imageNamed:@"image.png"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(BackButtonPressed)
  forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.leftBarButtonItem=barButton;
Saurav Mac
  • 82
  • 5
  • This doesn't seem to work either, I have changed 'image.png' to an image I was using for something else and still it doesn't show. – Agamemnon Jan 20 '14 at 09:27
  • Please check carefully that the image you are using is actually in png format and the image name should be in double quotes . – Saurav Mac Jan 20 '14 at 09:31
  • 1
    I'm using: "menuButton.png". Also, why won't the title show? I think that something is wrong that is stopping the title and the buttons showing. – Agamemnon Jan 20 '14 at 09:34
  • Then there must be some problem with your navigation bar . Track your navigation bar carefully , how have you allocated it for that particular view . – Saurav Mac Jan 20 '14 at 09:38
  • Can you please send your code you are using to alloc bar button . – Saurav Mac Jan 20 '14 at 09:39
  • Please check out this link , it may solve your purpose [link] (http://stackoverflow.com/questions/2266300/navigationitem-settitle-doesnt-work-for-second-level-uiviewcontroller) – Saurav Mac Jan 20 '14 at 09:50
  • You have just reminded me that there is another ViewController BEFORE the mainViewController, I think I have to add buttons there? – Agamemnon Jan 20 '14 at 09:56
  • Yes if there is any sub view controller then you have to add buttons on that View controller rather than the main view controller . – Saurav Mac Jan 20 '14 at 09:58
0
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;

Change it to

self.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationItem.rightBarButtonItem = showSettingsButton;
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
0
    try This ...

    In AppDelegate.h

    @property (strong, nonatomic) UINavigationController *nav; //navigationControlller Object


    AppDelegate.m


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        UIViewController* mainVC = [mainStoryboard instantiateInitialViewController];
        self.nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
        [self.window setRootViewController:self.nav];
        [_window makeKeyAndVisible];
        // Override point for customization after application launch.
        return YES;
    }

    One of the Views:

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [self setTitle:@"Congress app"];

        UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showSettings:)];

        self.navigationItem.leftBarButtonItem = showSettingsButton;

      }

-(void)showSettings:(id)sender
{
    NSLog(@"showSettings");
}
ggg_1120
  • 398
  • 2
  • 19
  • Have a observe the code..there is change in the following methods: 1)- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2)- (void)viewDidLoad – ggg_1120 Jan 20 '14 at 09:28
  • I have copied this code over mine it still doesn't work. I can't even get the title to show. – Agamemnon Jan 20 '14 at 09:32
  • Hey Ryan..Did you get any Solutions? – ggg_1120 Jan 20 '14 at 10:22
  • Hi, yes I did in the end. See my edits in the question - it was down to another the Navigation Controller being allocated in another View Controller. – Agamemnon Jan 20 '14 at 10:28
0

Try this one:

Display leftBarButtonItem of navigation controller:

    UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
    [face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
    self.navigationItem.leftBarButtonItem = backButton;
    [self.navigationItem setHidesBackButton:YES animated:YES];
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    [self.navigationItem setBackBarButtonItem:nil];

Display rightBarButtonItem of navigation controller:

UIImage *faceImage1= [UIImage imageNamed:@"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:@selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;

May be it will help you.

Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35
0

pls try this ...

- (void)viewDidLoad
{

     [super viewDidLoad];

     CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
     UIButton *button = [[UIButton alloc] initWithFrame:frameimgback1];
     [button addTarget:self action:@selector(back)  forControlEvents:UIControlEventTouchUpInside];
     [button setTitle:@"Ravindra" forState:UIControlStateNormal];
     UIBarButtonItem *btnL = [[UIBarButtonItem alloc] initWithCustomView:button];
     self.navigationItem.leftBarButtonItem = btnL;
}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59