将工具栏添加到导航视图控制器的正确方法是什么?
所以我试图将 UIToolbar 添加到 UIViewController 中,该 UIViewController 是 UINavigation 层次结构的一部分,我想知道执行此操作的最佳方法是什么。我知道在 iOS3 中,他们使属于导航层次结构一部分的每个视图控制器都拥有自己的工具栏,所以我认为这是最好的方法。然而,我正在做的语法让我烦恼,因为我使用三种不同类型的语法来添加工具栏,如下所示:
[[self navigationController] setToolbarHidden:NO];
[self setToolbarItems: myToolbarButtons];
[[[self navigationController] toolbar]setBarStyle:UIBarStyleBlack];
这工作正常,并且实际上修复了我将工具栏添加到时的错误内存访问导航视图的子视图。但我不明白在使工具栏可见后如何执行“self setToolbarItems”。那么它会成为视图控制器的一部分吗?就像我说的,这有效但困扰着我。
So I'm trying to add a UIToolbar to a UIViewController that is part of a UINavigation hierarchy and I was wondering what is the best way to do this. I know in iOS3 they enabled each viewcontroller that is part of a navigation hierarchy to have it's own toolbar so I figure this is the best way to do it. However, the syntax of how I'm doing is bugging me, as I'm using three different types of syntax to add the toolbar as follows:
[[self navigationController] setToolbarHidden:NO];
[self setToolbarItems: myToolbarButtons];
[[[self navigationController] toolbar]setBarStyle:UIBarStyleBlack];
This works fine, and actually fixes a bad memory access from when I was adding the toolbar to the subview of the navigation view. But I don't understand how I can do "self setToolbarItems" after I make the toolbar visible. Does it become a part of the viewcontroller then? Like I said, this works but is bugging me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIVIewController 有一个名为 UINavigationController 的属性。每个 UINavigationController 都有自己的内置工具栏。所以当你调用的时候,
实际上是启用了每个UIViewController自带的navigationController属性的工具栏。当您使用以下两行设置工具栏的项目和样式时:
您实际上是在设置 UINavigationController 的内置工具栏的项目。
希望这有帮助。查看 UIViewController 类参考了解更多信息。
An UIVIewController has a property called UINavigationController. Each UINavigationController has a built-in toolbar of their own. So when you are calling,
you are actually enabling the toolbar of the navigationController property that comes with each UIViewController. And when you set the toolbar's item and style with the following two lines:
you are actually setting the items of that UINavigationController's built-in toolbar.
Hope this helps. Check out the UIViewController Class Reference for more information.
在第 1 行中,您向工具栏的超级视图发送一条消息以隐藏它,这是通过这种方式完成的,因为这样超级视图可以在最佳时间执行它想要的任何调整大小/布局。
导航控制器在第 2 行中从
self
的变量中读取工具栏,因为导航控制器当前正在呈现self
。在第 3 行中,您将访问由
navigationController
管理的工具栏本身。这是工具栏的一部分,与自身相关,与导航控制器无关,因此导航控制器不会公开修改它的方法,允许您直接访问工具栏,然后对其进行修改。通过这种方式,您可以访问各个级别的工具栏,从而使您自己更轻松。对于第一行,您不必管理显示它。在第二行和第三行中,您不必管理创建/销毁它。
In line 1 you're sending a message to the toolbar's superview to hide it which is done this way because then the superview can do whatever resizing/layout it wants at the best time.
The navigation controller reads the toolbar from
self
's variables in line 2, because the navigation controller is currently presentingself
.In line 3 you're accessing the toolbar itself, managed by
navigationController
. This is a part of the toolbar that relates to itself and not the navigation controller, so the navigation controller doesn't expose ways to modify it, allowing you to access the toolbar directly and then modify it.This way you are accessing the toolbar at various levels making it easier on yourself. For In the first line you don't have to manage displaying it. In the second and third lines you don't have to manage creating/destroying it.