Cocoa SetBackgroundColor到NSView
我想念什么?颜色不会变Cocoa SetBackgroundColor到NSView,cocoa,initialization,nsview,init,nscolor,Cocoa,Initialization,Nsview,Init,Nscolor,我想念什么?颜色不会变 #import "controller.h" #import "backgroundView.h" @implementation controller -(void)awakeFromNib { backgroundView *background = [[backgroundView alloc] init]; [background setBackgroudColor:[NSColor whiteColor]]; //also didn't
#import "controller.h"
#import "backgroundView.h"
@implementation controller
-(void)awakeFromNib {
backgroundView *background = [[backgroundView alloc] init];
[background setBackgroudColor:[NSColor whiteColor]];
//also didn't work
//[background setBackgroudColor:[[NSColor whiteColor] retain]];
}
@end
//backgroundView.h
#import <Cocoa/Cocoa.h>
@interface backgroundView : NSView{
NSColor *color;
}
-(void)setBackgroudColor:(NSColor*)newColor;
@end
#import "backgroundView.h"
@implementation backgroundView
-(void)dealloc{
[super dealloc];
}
-(void)setBackgroudColor:(NSColor*)newColor{
color = newColor;
[self setNeedsDisplay:YES];
}
-(void)drawRect:(NSRect)rect{
[color setFill];
NSRectFill(rect);
}
@end
#导入“controller.h”
#导入“backgroundView.h”
@执行控制器
-(无效)从NIB中唤醒{
backgroundView*background=[[backgroundView alloc]init];
[背景背景背景颜色:[NSColor whiteColor]];
//也没用
//[背景背景背景颜色:[[NSColor WHITECLOR]retain];
}
@结束
//背景视图
#进口
@接口背景视图:NSView{
NSColor*颜色;
}
-(无效)退根颜色:(NSColor*)新颜色;
@结束
#导入“backgroundView.h”
@实现背景视图
-(无效)解除锁定{
[super dealoc];
}
-(无效)收进地面颜色:(NSColor*)新颜色{
颜色=新颜色;
[自我设置需要显示:是];
}
-(void)drawRect:(NSRect)rect{
[颜色设置填充];
NSRectFill(rect);
}
@结束
setBackgroundColor:
方法中保留newColor
dealloc
init
初始化视图,但指定的初始值设定项是initWithFrame:
NSColor
您正在
awakeFromNib
中创建一个视图,该视图没有附加到任何位置。相反,您应该在Interface Builder中更改视图的自定义类,在此视图上设置一个出口,并在其上调用setbackgroundColor:
此外,类应该以大写字母开头,因此
backgroundView
应该是backgroundView
。正如Andrey所说,请确保修复颜色ivar的内存管理。您能给我一个如何使用initWithFrame的示例吗?@user840250:您使用它的方式与init
相同,但您将希望视图作为其框架的矩形传递给它。有关更多信息,请参阅《视图编程指南》:和NSView类参考: