将UIBarButtonItem添加到UINavigationB

将UIBarButtonItem添加到UINavigationB

本文介绍了以编程方式将UIBarButtonItem添加到UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIInterfaceBuilder 中放入 UINavigationBar 。我以模态方式呈现此视图,只想要 UIBackBarButton 返回上一个视图。我有一个出口和属性来声明这个 UINavigationBar 。我想在我的 viewDidLoad 方法中,我可以像这样创建一个 UIBackButton

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:

UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
                                              target:self
                                              action:@selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

但是我没有看到我的 UIBackBarButtonItem UINavigationBar 。我认为我在这里做错了,因为我不认为我的 UINavigationBar 知道我正在尝试添加这个 UIBackBarButtonItem 以这种方式。我是否必须创建一个 NSArray ,将按钮放入其中,并为NavigationBar设置setItems?

But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?

我对于navigationItem属性如何工作以及 UINavigationBar 的setItems感到困惑。任何帮助,将不胜感激。谢谢!

I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!

推荐答案

您正在尝试在模式视图中设置后退按钮项,该视图不会添加backBarButtonItem。这导致按钮(或任何类型的后退按钮)不显示。 backBarButtonItem主要用于推送视图控制器,当您按下新视图控制器(顶部项目)时,它会从父节点(下面的下一项)添加后退按钮。 说:

You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:

要获得左侧的后退按钮,尝试更改

To get the Back Button on the left side like you wish, Try changing

self.navigationItem.backBarButtonItem = backButton;

self.navigationItem.leftBarButtonItem = backButton;

这篇关于以编程方式将UIBarButtonItem添加到UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:22