尝试以编程方式向UINavigationController添加一个按钮但它永远不会显示

dig*_*rok 7 iphone objective-c uinavigationcontroller uibarbuttonitem ios

我以编程方式创建了一些UINavigationControllers并将它们添加到UITabBarController中.一切似乎工作正常,但我想在导航控制器中添加一个取消按钮,但它永远不会出现.我尝试了多种方法,但我似乎根本无法影响导航项目的显示,我已经从这里和其他网站跟踪了多个示例,但没有任何反应.

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
Run Code Online (Sandbox Code Playgroud)

我也试过这样添加按钮

[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO];  // also doesn't do anything
Run Code Online (Sandbox Code Playgroud)

我开始感到沮丧,所以我也尝试了一些其他的事情,看看我是否可以影响任何事情,但无济于事

myNavController.title = @"test";  // does nothing
Run Code Online (Sandbox Code Playgroud)

我已尝试在将导航控制器添加到UITabBarController之前和之后执行此操作并且没有帮助.我也尝试过rightBarButtonItem并尝试使用initWithTitle:而不是initWithBarButtonSystemItem.

有人请照亮我吗?显然,我这样做是错误的.

Emp*_*ack 16

尝试在下面的loadView方法中添加条形按钮MyTableViewController.

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

我想这应该有效.