1

I have the following code where after a bool is true I want to add a drawing to my rect. here is the code I have but for some reason it is either not setting the bool or calling the setNeedsDisplay. Am I referencing to the other class properly? thanks

//in AppController.m

-(IBAction)colorToggle:(id)sender
{
    if ([colorFilter state] == NSOnState) 
    {
        CutoutView *theView = [[CutoutView alloc] init];
        [theView setFilterEnabled:YES];

    }

}

//in cutoutView.m

- (void)drawRect:(NSRect)dirtyRect
{
    [[[NSColor blackColor]colorWithAlphaComponent:0.9]set];
    NSRectFill(dirtyRect); 

    //this is what i want to be drawn when my bool is true and update the drawRect        
    if (filterEnabled == YES) {
        NSRectFillUsingOperation(NSMakeRect(100, 100, 300, 300), NSCompositeClear);
        [self update];
    }
}

-(void)update
{
    [self setNeedsDisplay:YES];
}
4

2 回答 2

2

好的,你知道不是每个 UILabel 都是一样的吗?就像,您可以从视图中删除一个 UILabel 而不让所有其他 UILabel 也消失?好吧,您的 CutoutView 也是如此。当您在那里编写CutoutView *theView = [[CutoutView alloc] init];时,这会创建一个不会在任何地方显示的CutoutView。您需要与现有的CutoutView 交谈(可能通过连接插座,但有许多完全有效的设计可以实现此目标)。

于 2012-04-04T04:02:32.923 回答
0

您忘记调用该drawRect:方法,它应该如下所示:

CutoutView *theView = [[CutoutView alloc] init];
[theView setFilterEnabled:YES];
[theView setNeedsDisplay];

文档

当您的视图的实际内容发生变化时,您有责任通知系统您的视图需要重绘。为此,您可以调用视图的 setNeedsDisplay 或 setNeedsDisplayInRect: 方法。

于 2012-04-04T03:44:16.103 回答