如何以编程方式添加 UIBarButtonItem?

发布于 2024-10-26 22:45:27 字数 692 浏览 0 评论 0原文

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

这是我当前的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

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

我希望有人能引导我走向正确的方向。我尝试调用它的控制器有 3 个级别。因此,UITabBarController -> UIViewController(设置,第一级) -> UIViewController(车辆,第二层) -> UIViewController(库存,第三层)

无论如何,预先感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-11-02 22:45:27

[[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将被系统忽略。 (但这不应该是它不起作用的原因。)

[[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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文