让 NSView 以编程方式出现在 Objective-C 中

发布于 2024-11-29 20:24:54 字数 978 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑寡妇 2024-12-06 20:24:54

我刚刚了解到,视图只能位于一个超级视图中。我有测试代码和我想要工作的代码,因此它删除了我的视图并将其放在窗口中。

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.

彩虹直至黑白 2024-12-06 20:24:54

该自定义视图(其中包含文本)有一个自定义 drawRect 实现,在其中绘制文本,对吧?在这种情况下,我的想法是您需要调用 superdrawRect 实现来确保子视图也被绘制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文