Setting Bool in different classes

Go To StackoverFlow.com

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];
}
2012-04-04 03:22
by Grant Wilkinson


2

OK, you know how not every UILabel is the same? Like, you can remove one UILabel from a view without all the others disappearing too? Well, your CutoutView is the same way. When you write CutoutView *theView = [[CutoutView alloc] init]; there, that creates a new CutoutView that isn't displayed anywhere. You need to talk to your existing CutoutView (probably by hooking up an outlet, but there are any number of perfectly valid designs that will accomplish this goal).

2012-04-04 04:02
by Chuck
Thanks! that made perfect sens - Grant Wilkinson 2012-04-04 04:14


0

You are forgetting to call the drawRect: method, it should looks like this:

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

From the docs:

When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay or setNeedsDisplayInRect: method of the view.

2012-04-04 03:44
by El Developer
Thanks I get what you mean but even when I use [theView setNeedsDisplay:YES]; it still isint redrawing the rect. Is my bool statement correct - Grant Wilkinson 2012-04-04 03:50
Ads