setBackgroudColor to NSView
What I miss? color won't change.
#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)setBac开发者_JAVA百科kgroudColor:(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
- You should retain
newColor
insetBackgroundColor:
method. - Release
color
ivar indealloc
- In awakeFromNib method you initialize your view with
init
, but designated initializer isinitWithFrame:
- There's no code where you add newly created view to superview.
- You can also try to use
set
instead ofsetFill
forNSColor
You are creating a view in awakeFromNib
which is attached to nowhere. Instead you should change the custom class of your view in Interface Builder, setup an outlet on this view and call setBackgroudColor:
on it.
Also, classes should start with a capital letter so backgroundView
should be BackgroundView
. As Andriy said, make sure to fix the memory management of your color
ivar.
精彩评论