我在NSView的自定义类中有此选项,当我使用drawMyRec IBAction按下按钮时,我想更改NSRect的颜色,但是这种方法不起作用,有人可以帮忙吗?

#import "myView.h"

@implementation myView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        RectColor = [[NSColor blackColor] init];
    }

    return self;
}


- (IBAction)drawMyRec:(id)sender;
{
    NSLog(@"pressed");
    RectColor = [[NSColor blueColor] init];
    [self setNeedsDisplay:YES];
}


- (void)drawRect:(NSRect)dirtyRect
{
    NSRectFill(dirtyRect);
    [RectColor set];

}

@end

最佳答案

您的drawRect函数不正确。更改为此:

- (void)drawRect:(NSRect)dirtyRect
{
    [RectColor set];
    NSRectFill(dirtyRect);

}

关于xcode - 按下按钮后setNeedsDisplay for DrawRect,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9761208/

10-10 17:19