简体   繁体   English

如何在Objective-c中引用/打开一个窗口

[英]How to reference/open a window in Objective-c

I need to get a reference to the single window in my app. 我需要获取我应用程序中单个窗口的引用。 If I tick off "Visible at launch" it shows up. 如果我勾选“启动时可见”,则会显示出来。

I keep seeing code like the following but that doesn't make much sense. 我一直在看如下代码,但这没有多大意义。 How would self know about a window in a nib file if its not declared anywhere in the interface? 如果未在接口中的任何位置声明nib文件中的窗口,自我将如何知道?

NSWindow *window = [self window]; 

When I run that in applicationDidFinishLaunching, I get this error: 当我在applicationDidFinishLaunching中运行它时,出现此错误:

No visible @interface for 'AppDelegate' declares the selector 'window'

That makes sense, because no interface is declared. 这是有道理的,因为没有声明任何接口。 Yet how is the window loading automatically when 'Visible at launch' is ticked? 但是,勾选“启动时可见”时,窗口如何自动加载?

My end goal is that I need to reference a window and change its level, ie 我的最终目标是我需要引用一个窗口并更改其级别,即

[window setLevel:NSFloatingWindowLevel];

Please help? 请帮忙?

The short answer is: 简短的答案是:

In AppDelegate.h, declare a "window" property: 在AppDelegate.h中,声明一个“窗口”属性:

 @property (strong) IBOutlet NSWindow* window;

In MainMenu.nib, connect your main window to the "window" property of "AppDelegate": 在MainMenu.nib中,将主窗口连接到“ AppDelegate”的“ window”属性:

Right-click on the "AppDelegate" object, then click on the widget next to the "window" property and drag it to your window. 右键单击“ AppDelegate”对象,然后单击“窗口”属性旁边的小部件,然后将其拖动到窗口中。

Your "AppDelegate" instance will now have a "window" property, and it will be assigned to your main window. 您的“ AppDelegate”实例现在将具有“ window”属性,并将其分配给您的主窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM