在不同的班级设置布尔(Setting Bool in different classes)

2019-10-16 13:53发布

我有下面的代码,后一个布尔是真实的我想一个绘图添加到我的矩形。 这里是我的代码,但由于某种原因,或者未设置布尔或调用setNeedsDisplay。 我在引用其他类是否正确? 谢谢

//在AppController.m

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

    }

}

//在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];
}

Answer 1:

OK,你知道每一个的UILabel如何不相同? 喜欢的话,可以从视图中删除一个没有的UILabel所有的人消失了吗? 那么,你的CutoutView也是同样的道理。 当你写CutoutView *theView = [[CutoutView alloc] init]; 那里,创建一个没有任何地方显示一个新的 CutoutView。 你需要跟你的现有 CutoutView(可能是通过连接一个出口,但也有任意数量的完全有效的设计,将实现这一目标)。



Answer 2:

您忘记调用drawRect:方法,它应该是这样的:

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

从文档 :

当您的视图更改的实际内容,这是你的责任通知你的观点需要重新绘制系统。 视图的方法:你可以通过调用视图的setNeedsDisplay或setNeedsDisplayInRect做到这一点。



文章来源: Setting Bool in different classes