如何以编程方式添加 UIBarButtonItem? [英] How to add a UIBarButtonItem programmatically?

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

问题描述

以编程方式添加 UIBarButtonItem 的正确方法是什么?就我而言,我正在尝试将一个添加到 rightBarButtonItem 并且我一直在控制器层次结构中跳来跳去,但我似乎无法让按钮显示在任何地方.

这是我当前的代码:

- (void)viewDidLoad {[超级viewDidLoad];[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:@"Barcode-White.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scanEquipment:)] autorelease]];}

我希望有人能引导我走向正确的方向.我试图从中调用它的控制器是 3 个级别.因此,UITabBarController -> UIViewController (Settings, 1st level) -> UIViewController (Vehicle,2nd level) -> UIViewController (Inventory, 3rd level).

无论如何,提前感谢您的帮助!

解决方案

[[UIImage alloc] initWithContentsOfFile:@"Barcode-White.png"] 可能不起作用.initWithContentsOfFile 获取图像文件的完整路径,而不仅仅是文件名.这可能是问题所在;它返回 nil,这导致整个按钮构造函数返回 nil.

(此外,您通过调用没有发布或自动发布的 init 方法来泄漏此图像.)

尝试 [UIImage imageNamed:@"Barcode-White"] 代替,它会在应用程序的资源中查找图像文件,并且具有仅加载一次图像然后缓存它的额外好处无论调用多少次,都在内存中:

http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageNamed::>

除此之外,它看起来应该可以工作......

此外,导航栏项的样式始终为 UIBarButtonItemStyleBordered.尝试将其设置为 UIBarButtonItemStylePlain 将被系统忽略.(但不应该是它不起作用的原因.)

what's the proper way to add a UIBarButtonItem programmatically? In my case I'm trying to add one to the rightBarButtonItem and I've been hopping round the controller hierarchy but I can't seem to get the button to show up anywhere.

Here's my current code:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:@"Barcode-White.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scanEquipment:)] autorelease]];
}

I'm hoping someone can guide me into the right direction. The controller I'm trying to invoke this from is 3 levels in. So, UITabBarController -> UIViewController (Settings, 1st level) -> UIViewController (Vehicle, 2nd level) -> UIViewController (Inventory, 3rd level).

Anyway, thanks in advance for any help!

解决方案

[[UIImage alloc] initWithContentsOfFile:@"Barcode-White.png"] probably won't work. initWithContentsOfFile takes a complete path to an image file, not just a file name. That's probably the problem; it's returning nil, which is causing the whole button constructor to return nil.

(Also, you're leaking this image by calling an init method without a release or autorelease.)

Try [UIImage imageNamed:@"Barcode-White"] instead, which looks for an image file in the app's resources, and has the added bonus of only loading the image once and then caching it in memory no matter how many times it's called:

http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageNamed:

Other than that, it looks like it should work...

Also, navigation bar items always have a style of UIBarButtonItemStyleBordered. Trying to set it to UIBarButtonItemStylePlain will be ignored by the system. (But shouldn't be the reason it's not working.)

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

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