Setting Toolbar Items of UINavigationController

 ̄綄美尐妖づ 提交于 2019-11-29 00:10:10

It's a perfectly acceptable way of doing it, but do bear in mind that loading xib files is quite expensive on the iPhone, and it may well be faster to create the toolbar items programatically in your viewDidLoad method.

I don't know if this is documented anywhere, but I've found that in Interface Builder, if you enable the navigation controller's toolbar, you can drag bar items to your view controller, and they will automagically show up in the navigation controller's toolbar.

For example, here's what we can do (using Xcode 3.2 on Snow Leopard):

  1. File->New Project.... Choose Navigation-based Application and create the project.
  2. Open MainWindow.xib in Interface Builder.
  3. Select the Navigation Controller, and in the Attributes inspector, check the "Shows Toolbar" box. This will cause a Toolbar object to appear.
  4. Drag a Bar Button Item from the Library to the toolbar. It will appear in the toolbar. If you check the hierarchy in the NIB, you'll see that this new item is a child of the RootViewController.

It seems that any Bar Button Items added as children of the navigation item will show up in the navigation bar, and any Bar Button Items added as children of the view controller will show up in the toolbar.

(I stumbled on this by accident. If anyone can find documentation for this behavior, or any additional info, I'd like to hear about it.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!