让 NSView 以编程方式出现在 Objective-C 中
我有一个 NSView 对象,它作为函数的结果返回给我。我知道视图是有效的,因为如果我这样做,我可以看到视图的内容:
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];
在我的应用程序中,我有一个带有自定义视图的窗口(上面有一些文本),该窗口在我的代码中使用 IBOutlet 引用了一个插座。我正在尝试添加作为该出口的子视图返回的视图。
[referencedView addSubView:returnedView]
[referencedView setNeedsDisplay:YES];
引用的视图是可见的(我可以看到其中的文本),但返回的视图不会出现在顶部。我是不是忘记了什么?
这就是我的代码现在的样子:
[returnedView setFrame:NSMakeRect(0,0,200,200)];
[referencedView addSubview:returnedView];
[referencedView setNeedsDisplay:YES];
[referencedView drawRect:[referencedView bounds]];
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.
[referencedView 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]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚了解到,视图只能位于一个超级视图中。我有测试代码和我想要工作的代码,因此它删除了我的视图并将其放在窗口中。
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.
该自定义视图(其中包含文本)有一个自定义
drawRect
实现,在其中绘制文本,对吧?在这种情况下,我的想法是您需要调用super
的drawRect
实现来确保子视图也被绘制。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 callsuper
's implementation ofdrawRect
to make sure that the subviews get drawn too.