我有以下代码,其中 bool 为 true 后我想将绘图添加到我的矩形中。这是我的代码,但由于某种原因,它要么没有设置 bool,要么调用 setNeedsDisplay。我是否正确引用了其他类(class)?谢谢
//在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];
}
最佳答案
好吧,你知道为什么不是每个 UILabel 都是一样的吗?例如,您可以从 View 中删除一个 UILabel 而不会导致其他所有标签也消失?嗯,你的 CutoutView 也是一样的。当您在那里编写 CutoutView *theView = [[CutoutView alloc] init];
时,会创建一个不显示在任何地方的新 CutoutView。您需要与您的现有 CutoutView 进行对话(可能通过连接一个 socket ,但有许多完全有效的设计可以实现此目标)。
关于objective-c - 在不同的类中设置 Bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004502/