开发者

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


  1. You should retain newColor in setBackgroundColor: method.
  2. Release color ivar in dealloc
  3. In awakeFromNib method you initialize your view with init, but designated initializer is initWithFrame:
  4. There's no code where you add newly created view to superview.
  5. You can also try to use set instead of setFill for NSColor


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜