无法设置 NSWindow 的位置
我有一个扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Wevah 的想法是正确的,不过我会尝试对其进行一些扩展。
如果您尝试将此行添加到
initWithWindowNibName:
方法中:您可能会在控制台中看到以下输出:
换句话说,窗口仍然是
nil
,如 < code>init* 方法在对象生命周期的早期阶段,许多IBOutlet
或用户界面项尚未完全“连接”。向
nil
发送消息完全没问题:它会被忽略。因此,基本上您尝试定位窗口没有任何效果,因为它基本上等同于[nil doSomething]
;关键是稍后在控制器对象的生命周期中执行窗口的定位,其中 IBOutlet 和其他用户界面对象正确连接。正如 Wevah 提到的,正确连接事物的一种方法是
或者在
NSWindowController
的情况下,也是以下一种:希望这会有所帮助......
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:You would likely see the following output to console:
In other words, the window is still
nil
, asinit*
methods are so early on in an object's lifetime that manyIBOutlet
s 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
IBOutlet
s and other user interface objects are properly hooked up. As Wevah alluded to, one such method where things are properly hooked up isor in the case of
NSWindowController
, the following one as well:Hope this helps...
尝试将该代码放入
awakeFromNib
中。Try putting that code in
awakeFromNib
.