Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa SetBackgroundColor到NSView_Cocoa_Initialization_Nsview_Init_Nscolor - Fatal编程技术网

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
  • 在awakeFromNib方法中,您使用
    init
    初始化视图,但指定的初始值设定项是
    initWithFrame:
  • 没有将新创建的视图添加到superview的代码
  • 对于
    NSColor

  • 您正在
    awakeFromNib
    中创建一个视图,该视图没有附加到任何位置。相反,您应该在Interface Builder中更改视图的自定义类,在此视图上设置一个出口,并在其上调用
    setbackgroundColor:


    此外,类应该以大写字母开头,因此
    backgroundView
    应该是
    backgroundView
    。正如Andrey所说,请确保修复
    颜色
    ivar的内存管理。

    您能给我一个如何使用initWithFrame的示例吗?@user840250:您使用它的方式与
    init
    相同,但您将希望视图作为其框架的矩形传递给它。有关更多信息,请参阅《视图编程指南》:和NSView类参考: