2012-04-04 42 views
1

我有下面的代码,后一个布尔是真实的我想一个绘图添加到我的矩形。这里是我有的代码,但由于某些原因,它不是设置bool或调用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]; 
} 

回答

2

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

+0

谢谢!这非常有意义 – 2012-04-04 04:14:51

0

你忘记打电话给drawRect:方法,它应该是这样的:

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

docs

当您的视图更改的实际内容,这是你的 责任通知你的观点必须 重绘系统。您可以通过调用视图的setNeedsDisplay或 setNeedsDisplayInRect:视图的方法来执行此操作。

+0

感谢我得到你的意思,但即使当我用'[theView setNeedsDisplay:YES];'它仍然isint重绘矩形。我的布尔声明是正确的吗? – 2012-04-04 03:50:04