adit
adit

Reputation: 33674

issue adding a leftBarButtonItem to nav bar

I have a UINavigationController with my AHViewController as the root view controller.

Then I tried to add a left bar button item to it from the viewDidLoad of AHViewController:

UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"grid.png"] forState:UIControlStateNormal];
    UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    //[self.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [backButtonBarItem release];

However, I am not seeing anything. Why is this?

Upvotes: 0

Views: 825

Answers (2)

user2941395
user2941395

Reputation: 109

Thanks "Simon Golden" Answer Work for me.

Before: (This not show up my button)

self.navigationItem.LeftBarButtonItem: backButtonItem;

After: (This Work for me and the button show up)

[self.navigationItem setLeftBarButtonItem: backButtonItem];

If the StoryBoard didn't have LeftBarButtonItem and RightBarButtonItem in the ViewController, We have to set the Button(setLeft/RightBarButtonItem).

Otherwise just simple change the attribute of the Item (self.navigationItem.Left/RightBarButtonItem)

Upvotes: 0

Simon Goldeen
Simon Goldeen

Reputation: 9080

Instead of:

[self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];

Do this:

[self.navigationItem setLeftBarButtonItem:backButtonBarItem];

UINavigationController uses the navigationItem of the top view controller, not its (the UINavigationController's) navigation item property.

Upvotes: 1

Related Questions