Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to programmatically add a button to UINavigationController but it never shows up

I've programmatically created some UINavigationControllers and added them to a UITabBarController. Everything seems to work fine but I wanted to add a cancel button to the navigation controller but it never shows up. I've tried multiple ways but I can't seem to affect the display of the navigation items at all and I've followed multiple examples from here and other sites but nothing happens.

MyTableViewController *mtvc = [[MyTableViewController alloc] init]; 
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;  // this works
[mtvc release];

// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease]; 
myNavController.navigationItem.leftBarButtonItem = closeButton;  // never shows up

I also tried adding the button this way

[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO];  // also doesn't do anything

I started getting frustrated so I also tried some other things just to see if I could affect anything, but to no avail

myNavController.title = @"test";  // does nothing

I've tried doing it before and after the navigations controllers were added to the UITabBarController and that didn't help. I've also tried rightBarButtonItem and tried using initWithTitle: instead of initWithBarButtonSystemItem.

Would someone please illuminate me? Clearly, I'm doing this the wrong way.

like image 845
digarok Avatar asked Feb 21 '11 03:02

digarok


People also ask

What is uinavigationcontroller UIViewController?

@MainActor class UINavigationController : UIViewController A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.

How do I create a button in the navigation bar?

The initial bar button is defined in the storyboard, by dragging a UIBarButtonItem out of the object library and into the navigation bar. The sample also shows how to create and add each button type using code.

How do I set the uinavigationbar’s prompt?

Here’s how to set the navigation item’s prompt: Apply a custom background to a UINavigationBar by adding a bar tint color or background image. The sample sets the background image of a navigation bar like this:

How does the navigation controller update the navigation bar?

Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class.


1 Answers

Try adding the bar buttons in the loadView method of MyTableViewController like the follwing.

UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease]; 
self.navigationItem.leftBarButtonItem = closeButton;

I guess that should work.

like image 140
EmptyStack Avatar answered Nov 15 '22 19:11

EmptyStack