Grant Wilkinson
Grant Wilkinson

Reputation: 1128

Make View and Sub View Programmatically

I have the following code to create my window, my view and my sub view programmatically. The problem is my subview "filterView2" when it is added [filterView addSubview:filterView2]; crashes on that line. Is there something I forgot to include or did wrong? thanks!

    NSRect mainFrame = [[NSScreen mainScreen] frame];
    NSRect helpFrame = NSZeroRect;
    float width = 600;
    float height = 400;
    helpFrame.origin.x = (mainFrame.size.width - width) / 2.0;
    helpFrame.origin.y = 260.0;
    helpFrame.size.width = width;
    helpFrame.size.height = height; 

    helpWindow2 = [[BrightnessView windowWithFrame:helpFrame] retain];

    // Configure window.
    [helpWindow2 setReleasedWhenClosed:YES];
    [helpWindow2 setHidesOnDeactivate:NO];
    [helpWindow2 setCanHide:NO];
    [helpWindow2 setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
    [helpWindow2 setIgnoresMouseEvents:YES];
    [helpWindow2 setBackgroundColor:[NSColor clearColor]];
    [helpWindow2 setOpaque:NO];


    // Configure contentView.
    NSView *filterView = [helpWindow2 contentView];
    [filterView setWantsLayer:YES];
    //add subview
    NSView *filterView2 = [helpWindow2 contentView];
    [filterView addSubview:filterView2];

    //CALayer for filterView
    CALayer *theLayer = [CALayer layer];
    theLayer.opacity = 0;
    [filterView setLayer:theLayer];
    CGColorRef bgColor = CGColorCreateGenericRGB(0, 200, 255, 1);
    theLayer.backgroundColor = bgColor;
    CGColorRelease(bgColor);
    theLayer.borderColor = CGColorGetConstantColor(kCGColorWhite);
    theLayer.cornerRadius = 8.0;

    float helpOpacity = (([NSApp isActive] ? 1 : 0));
    [[[helpWindow2 contentView] layer] setOpacity:helpOpacity];


    [window addChildWindow:helpWindow2 ordered:NSWindowAbove];

Upvotes: 0

Views: 216

Answers (1)

Costique
Costique

Reputation: 23722

I think filterView and filterView2 are the same object, which causes an exception. You cannot add a view as a subview of itself.

Upvotes: 2

Related Questions