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

发布于 2024-10-18 11:24:50 字数 1231 浏览 2 评论 0原文

我以编程方式创建了一些 UINavigationController 并将它们添加到 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

我也尝试以这种方式添加按钮,

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

但我开始感到沮丧,所以我还尝试了其他一些事情,只是为了看看是否会影响任何东西,但无济于事,

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

我在导航控制器添加到 UITabBarController 之前和之后都尝试过这样做那没有帮助。 我还尝试过 rightBarButtonItem 并尝试使用 initWithTitle: 而不是 initWithBarButtonSystemItem。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜声 2024-10-25 11:24:51

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

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

我想这应该可行。

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.

享受孤独 2024-10-25 11:24:51

您是否尝试过像这样设置当前视图控制器的导航项的按钮:

mtvc.navigationItem.leftBarButtonItem = closeButton;

Have you tried setting the button of the current view controller's navigation item like this:

mtvc.navigationItem.leftBarButtonItem = closeButton;
鸢与 2024-10-25 11:24:51

如果您需要在 Swift 3.0 中执行此操作,那就很简单:

let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel,
                        target: self,
                        action: #selector({Your-viewController}.{close-method}))
self.navigationItem.leftBarButtonItem = closeButton

If you need to do that in Swift 3.0, it is simple like that:

let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel,
                        target: self,
                        action: #selector({Your-viewController}.{close-method}))
self.navigationItem.leftBarButtonItem = closeButton
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文