设置 UINavigationController 的工具栏项 [英] Setting Toolbar Items of UINavigationController

查看:23
本文介绍了设置 UINavigationController 的工具栏项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPhone OS 3.0 中,您可以使用 setToolbarItems:animated: 方法设置 UINavigationController 的工具栏项.但是,这需要您传入一个 UIToolbarItems 数组.虽然我可以通过编程方式创建这些工具栏项,但如果可能的话,我宁愿在 Interface Builder 中创建它们.

In iPhone OS 3.0, you can set the toolbar items of a UINavigationController using the setToolbarItems:animated: method. However, this requires you pass in an array of UIToolbarItems. While I could programmatically create these toolbar items, I'd rather create them in Interface Builder if possible.

考虑到这一点,我在MyGreatViewController.xib"中创建了一个 UIToolbar,并在其中填充了所需的工具栏项.然后,在MyGreatViewController.m"中,我从工具栏中获取项目并将它们传递给 setToolbarItems:animated::

With this in mind, I have created a UIToolbar in "MyGreatViewController.xib" and have populated it with the wanted toolbar items. Then, in "MyGreatViewController.m", I get the items from the toolbar and pass them to setToolbarItems:animated::

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setToolbarItems: [toolbar items]];
}

...其中 toolbar 是一个引用 UIToolbar 的 IBOutlet.

...where toolbar is an IBOutlet referring to the UIToolbar.

这是一个好方法吗?有没有更好的方法来实现这一点?我应该只以编程方式创建项目吗?

Is this a good approach? Is there a better way to accomplish this? Should I just create the items programmatically?

推荐答案

这是一种完全可以接受的方式,但请记住,在 iPhone 上加载 xib 文件非常昂贵,而且创建速度可能更快在您的 viewDidLoad 方法中以编程方式工具栏项.

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.

这篇关于设置 UINavigationController 的工具栏项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆