无法设置 NSWindow 的位置

发布于 2024-09-25 23:10:34 字数 307 浏览 2 评论 0原文

我有一个扩展 NSWindowController 的类,我正在尝试定位它控制的窗口。该窗口正确显示了所有预期的内容和功能,但是当我尝试在 initWithWindowNibName 方法中在屏幕上定位其起始位置时,该位置不会改变。这是代码:

NSPoint p = NSMakePoint(100, 50);
[[self window] setFrameTopLeftPoint:p];

这看起来非常简单,我不确定问题是什么。

感谢您的任何想法。

(发现问题了。我没有将窗口连接到IB中的类。)

I have a class that extends NSWindowController and I am trying to position the window it controls. The window displays all of the expected contents and functions correctly, but when I try and position its starting location on the screen in the initWithWindowNibName method, the position does not change. Here is the code:

NSPoint p = NSMakePoint(100, 50);
[[self window] setFrameTopLeftPoint:p];

This seems very straight forward and I'm not sure what the problem is.

Thanks for any ideas.

(Found the problem. I did not have the window wired up to the Class in IB.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天气好吗我好吗 2024-10-02 23:10:34

Wevah 的想法是正确的,不过我会尝试对其进行一些扩展。

如果您尝试将此行添加到 initWithWindowNibName: 方法中:

NSLog(@"window == %@", [self window]);

您可能会在控制台中看到以下输出:

window == (null)

换句话说,窗口仍然是 nil,如 < code>init* 方法在对象生命周期的早期阶段,许多 IBOutlet 或用户界面项尚未完全“连接”。

nil 发送消息完全没问题:它会被忽略。因此,基本上您尝试定位窗口没有任何效果,因为它基本上等同于 [nil doSomething];

关键是稍后在控制器对象的生命周期中执行窗口的定位,其中 IBOutlet 和其他用户界面对象正确连接。正如 Wevah 提到的,正确连接事物的一种方法是

- (void)awakeFromNib;

或者在 NSWindowController 的情况下,也是以下一种:

- (void)windowDidLoad;

希望这会有所帮助......

Wevah has the right idea, though I'll try to expand on it a bit.

If you were to try adding this line to your initWithWindowNibName: method:

NSLog(@"window == %@", [self window]);

You would likely see the following output to console:

window == (null)

In other words, the window is still nil, as init* methods are so early on in an object's lifetime that many IBOutlets or user interface items aren't quite "hooked up" yet.

Sending a message to nil is perfectly fine: it's simply ignored. So, basically your attempt to position the window has no effect because it basically equates to [nil doSomething];

The key then is to perform the positioning of the window later on in the controller object's lifetime, where the IBOutlets and other user interface objects are properly hooked up. As Wevah alluded to, one such method where things are properly hooked up is

- (void)awakeFromNib;

or in the case of NSWindowController, the following one as well:

- (void)windowDidLoad;

Hope this helps...

不回头走下去 2024-10-02 23:10:34

尝试将该代码放入 awakeFromNib 中。

Try putting that code in awakeFromNib.

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