简体   繁体   中英

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?

NSWindow *window = [self window]; 

When I run that in applicationDidFinishLaunching, I get this error:

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:

 @property (strong) IBOutlet NSWindow* window;

In MainMenu.nib, connect your main window to the "window" property of "AppDelegate":

Right-click on the "AppDelegate" object, then click on the widget next to the "window" property and drag it to your window.

Your "AppDelegate" instance will now have a "window" property, and it will be assigned to your main window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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