尝试以编程方式向 UINavigationController 添加按钮,但它从未显示
我以编程方式创建了一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在
MyTableViewController
的loadView
方法中添加栏按钮,如下所示。我想这应该可行。
Try adding the bar buttons in the
loadView
method ofMyTableViewController
like the follwing.I guess that should work.
您是否尝试过像这样设置当前视图控制器的导航项的按钮:
Have you tried setting the button of the current view controller's navigation item like this:
如果您需要在 Swift 3.0 中执行此操作,那就很简单:
If you need to do that in Swift 3.0, it is simple like that: