以编程方式将 UIBarButtonItem 添加到 UINavigationBar
我在 UIInterfaceBuilder
中放入了 UINavigationBar
。我以模态方式呈现此视图,只想要一个 UIBackBarButton
返回到我的上一个视图。我声明了这个 UINavigationBar
的插座和属性。我认为在我的 viewDidLoad 方法中,我可以创建一个像这样的 UIBackButton :
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:@selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
但是我在 UINavigationBar 上看不到我的 UIBackBarButtonItem /代码>。我认为我在这里做错了什么,因为我认为我的 UINavigationBar
不知道我正在尝试以这种方式添加此 UIBackBarButtonItem
。我是否必须创建一个 NSArray,将按钮放入其中,然后为导航栏设置项目?
我也对 navigationItem 属性与 UINavigationBar
的 setItems 的工作原理感到困惑。任何帮助将不胜感激。谢谢!
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];
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?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar
as well. Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试在不添加 backBarButtonItem 的模式视图中设置后退按钮项。这就是导致按钮(或任何类型的后退按钮)不显示的原因。 backBarButtonItem 主要用于推送视图控制器,当您推送新的视图控制器(顶部项目)时,该视图控制器会从父级(下面的下一项)添加后退按钮。 Apple UINavigationItem 文档 说:
要像您希望的那样将后退按钮放在左侧,请尝试更改
为
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
to
从视图控制器进行这样的调用
会将 NextViewController 作为调用视图上的模态视图呈现,并且 NextViewController 将为其提供一个 navigationController。
在 NextViewController 实现文件中,您所需要的只是
使用后退按钮来关闭模态视图。希望有帮助。
making a call such as this from a view controller
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
to have the back button to dismiss the modalview. Hope it helps.
使用下面的代码片段:
可用的不同样式类型有:
Use below code snippet :
Different style types available are :
您可以使用此设置器而无需创建新的 UIBarButtonItem:
You may use this setters without creation new UIBarButtonItem: