Getting NSView to appear programmatically in Objective-C
I have a NSView object that is being returned to me as a result of a function. I know the view is valid because I can see the contents of the view if I do this:
NSRect rect = NSMakeRect(600,600,200,200);
NSWindow *testWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:NSTexturedBackgroundWindowMask backing: NSBackingStoreBuffered defer:NO];
[[testWindow contentView] addSubview:returnedView];
[testWindow makeKeyAndOrderFront:NSApp];
In my application I have a window with a custom view (has some text on it) that has an outlet referenced in my code using IBOutlet. I'm trying to add the view I'm getting returned as a subview of that outlet.
[re开发者_如何学GoferencedView addSubView:returnedView]
[referencedView setNeedsDisplay:YES];
The referenced view is visible (I can see the text in it), but the returnedView doesn't appear on top. Am I forgetting something?
This is what my code looks like now:
[returnedView setFrame:NSMakeRect(0,0,200,200)];
[referencedView addSubview:returnedView];
[referencedView setNeedsDisplay:YES];
[referencedView drawRect:[referencedView bounds]];
Views can only be in ONE superview is what I just learned. I had the test code and the code I wanted to work so it was removing my view and putting it in the window instead.
That custom view (with the text in it) has a custom drawRect
implementation, in which the text is drawn, right? In this case, my idea is that you'd want to call super
's implementation of drawRect
to make sure that the subviews get drawn too.
精彩评论