Where is the window outlet in an NSDocument

吃可爱长大的小学妹 提交于 2021-02-20 04:23:05

问题


My app was converted from a non-document-based app to a document-based one. I did that by creating a subclass of NSDocument, called Document. I also created a Document.xib and set its "File's Owner" to Document.

Now in Document.xib, I can see there is a window outlet in its "File's Owner". I don't have a window outlet defined in Document. Where does it come from? I guess it is from the super class NSDocument, but I have no access to that variable in Document. What's up with this weird window outlet?


回答1:


Have a look at the documentation for -[NSDocument setWindow:]

This method is invoked automatically during the loading of any nib for which this document is the file’s owner, if the file’s owner window outlet is connected in the nib. You should not invoke this method directly, and typically you would not override it either.

NSDocument doesn't deal with NSWindows directly, but it keeps a list of NSWindowControllers that you can access via the -[NSDocument windowControllers] method. My guess is that when setWindow: gets called, it wraps the window in a new NSWindowController and adds it to the list.

You should be able to access the window with something like this:

NSWindowController* controller = self.windowControllers.lastObject;
NSWindow* window = controller.window;

I just made a new project to test it, and this works:

- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
    [super windowControllerDidLoadNib:aController];
    NSLog(@"%@", [self.windowControllers.lastObject window]);
}


来源:https://stackoverflow.com/questions/26580908/where-is-the-window-outlet-in-an-nsdocument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!