I use a xib file to show an NSWindow
named mainWindow - now I want to get a reference to mainWindow via code (e.g. NSWindow *mainWindow
). I can't find anything in the documentation, any pointers?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The xib file will have placeholder objects in it for the app delegate and / or the file's owner.
On the assumption that it has the app delegate in it, you can get a reference to the window or any object in the xib by
Declare a property in the app delegate of the right type and with IBOutlet as part of its type:
@property (weak) IBOutlet NSWindow* theWindow;
Locate the app delegate object in the xib. Click and drag it while the control key is pressed. You should get a line between the mouse pointer and the object.
- Drag on to the window and release the mouse button.
- You should see a list of the outlets in the app delegate. Select
theWindow
and your done.