以编程方式更改按钮的背景图像可可obj-c [英] Change background image of a button programmatically cocoa obj-c

查看:98
本文介绍了以编程方式更改按钮的背景图像可可obj-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建NSCollection View(在Cocoa中),其大小和外观上都有许多相同的按钮,但它们的图标除外(或者您可以将它们称为背景图片)。

I am creating NSCollection View (in Cocoa) with many identical buttons in size and appearance, except for their icons (or you could probably call them Background Images).

在我的xib文件中,将按钮绑定到集合视图(模型键路径:presentedObject.foto),并且数组控制器具有MyButton和Keys类:foto。

In my xib file I have the button binded to the Collection View (Model key path: representedObject.foto) and the Array Controller has class of MyButton and Keys: foto.

我使用NSImage属性创建了My Button类-这是正确的方法吗?
如何设置我在AppController.m中的项目中拥有的图像,以使其在运行我的应用程序时显示在按钮上?

I created My Button class with NSImage property - is this the right way to do it? How do I set an Image that I have in my project in AppController.m so that it appears on the button when running my app?

其他所有内容很好,因为我以前用Labels创建了NSCollectionView并且它起作用了。

Everything else should be fine, because I have previously create NSCollectionView with Labels and it worked.

MyButton.h

MyButton.h

#import <Cocoa/Cocoa.h>
@interface MyButton : NSButton
@property(retain, readwrite) NSImage *foto;
@end  

MyButton.m

MyButton.m

#import "MyButton.h"
@implementation MyButton
@end

AppController.h

AppController.h

@interface AppController : NSObject {IBOutlet NSArrayController *controller;}
@property (strong) NSMutableArray *modelArray;
@end

AppController.m

AppController.m

#import "AppController.h"
#import "MyButton.h"
@implementation AppController

- (void) awakeFromNib {
MyButton *FirstOne = [[MyButton alloc] init];
//FirstOne.foto = ???

_modelArray = [[NSMutableArray alloc] init];
[controller addObject:FirstOne];}

@end


推荐答案

您的图片在项目中的什么位置?如果它在.xcasset文件中,则可以使用以下命令设置图像:

Where is your image located in your project? If its in an .xcasset file, then you can set the image with:

FirstOne.image = [NSImage imageNamed:@"StatusBarIcon"];

您可以在此处了解有关将图像存储在资产中的信息。我建议您使用这些资产,因为它可以使您可以在项目中使用的所有图像保持井井有条,并且可以减少维护多种分辨率的图像的麻烦。

You can learn about storing images in assets here. I would recommend using these assets as it allows you to keep all the images that you may use in your project organised, and it helps reduce the hassle of maintaining images for multiple resolutions.

这篇关于以编程方式更改按钮的背景图像可可obj-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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