繁体   English   中英

以编程方式更改按钮的背景图像可可 obj-c

[英]Change background image of a button programmatically cocoa obj-c

我正在创建 NSCollection 视图(在 Cocoa 中),其中包含许多大小和外观相同的按钮,除了它们的图标(或者您可以将它们称为背景图像)。

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

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

其他一切都应该没问题,因为我之前已经用标签创建了 NSCollectionView 并且它有效。

我的按钮.h

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

我的按钮.m

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

应用程序控制器

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

应用程序控制器

#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 文件中,那么您可以使用以下命令设置图像:

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

您可以在此处了解如何在资产中存储图像 我建议使用这些资产,因为它可以让您组织项目中可能使用的所有图像,并有助于减少维护多种分辨率图像的麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM