iphone - 尝试以编程方式向 UINavigationController 添加按钮,但它从未显示

标签 iphone objective-c ios uinavigationcontroller uibarbuttonitem

我以编程方式创建了一些 UINavigationController 并将它们添加到 UITabBarController。一切似乎都正常,但我想向导航 Controller 添加一个取消按钮,但它从未出现过。我尝试了多种方法,但似乎根本无法影响导航项的显示,并且我已经遵循了此处和其他站点的多个示例,但没有任何反应。

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

我也试过用这种方式添加按钮

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

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

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

在将导航 Controller 添加到 UITabBarController 之前和之后,我都尝试过这样做,但没有帮助。 我也试过 rightBarButtonItem 并尝试使用 initWithTitle: 而不是 initWithBarButtonSystemItem。

有人可以照亮我吗?显然,我这样做是错误的方式。

最佳答案

尝试在 MyTableViewControllerloadView 方法中添加条形按钮,如下所示。

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

我想这应该可行。

关于iphone - 尝试以编程方式向 UINavigationController 添加按钮,但它从未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5062088/

相关文章:

iphone - 将图像存储到sqlite数据库中

ios - 以编程方式快速调用 drawRect()

ios - 标准 UIButton isAccessibilityElement 默认返回 NO

ios - 从谷歌 API 获取附近的地方按距离排序

ios - 具有两个 TableView 的 SplitviewController,委托(delegate)问题

iphone - 不更新 iPhone 应用程序捆绑版本的后果?

iphone - 在 iOS 应用中裁剪图像

iphone - 如何在 objective-c 中使用属性的实际示例?

ios - UIScrollView 似乎在 xcode 6 (iOS 8) 中不起作用

ios - 在IOS中的两个NavigationViewController之间添加Wizard风格的ViewController