0

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];
3
  • the helpWindow2 function "contentView", does that allocate a new version of a view or atleast retain it? and what kind of crash are we talking about here? EXC_BAD_aLLOC? Provide more info about the crash and how the "contentView" function/property/variable looks. need to know what it is Commented Apr 2, 2012 at 4:14
  • EXC_BAD_ACCESS is the error message and the variable is just the (id)contentView Commented Apr 2, 2012 at 4:45
  • 1
    usally when u get bad_access it's cus you are trying to access something that's not retained(retain count < 1) due to that you released the memory (for example). in this case you actually have to look at your design. should a view be it's own subview? doesn't really sound right to me, so try to create a new instance of the contentView that you put into your object, you can even do a copy of it so that you dont loose anything. Commented Apr 2, 2012 at 4:59

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

so then how can I turn filterView2 into strictly a subview?
The most straight-forward way to create an NSView is to call [[NSView alloc] initWithFrame: ...]. However, if filterView2 is a subclass of NSView, it may have a different designated initializer. You should know what kind of view you want filterView2 to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.