改变一个按钮的背景图像编程可可对象 [英] Change background image of a button programatically cocoa obj-c

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

问题描述

我创建NSCollection视图(在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文件中,我的按钮绑定到集合视图(模型键路径:resentObject.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?

其他应该很好,因为我以前创建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.

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

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