从NSDocument类访问主窗口

pme*_*ino 0 macos cocoa nsdocument nswindow

我有一个NSDocument类,我需要访问主菜单窗口,这是一个在应用程序启动时打开的窗口.当我从应用程序在该窗口中操作时似乎都工作,但是当尝试从readFromFileWrapper:ofType:error:窗口执行相同的操作时,我访问似乎是零.为什么会这样?

编辑:一些处理此问题的代码:

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError
{
    if([[NSFileManager alloc] fileExistsAtPath:[NSString stringWithFormat:@"%@/Project.plist",[[self fileURL] path]]]) {
        NSLog(@"%@", [[self fileURL] path]);

        NSDictionary *project = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Project.plist",[[self fileURL] path]]];

        if([[project objectForKey:@"type"] isEqualToString:@"vote"]) {

            [self openProject:[[self fileURL] path] type:@"vote"];

            return YES;

        } else if([[project objectForKey:@"type"] isEqualToString:@"quiz"]) {

            [self openProject:[[self fileURL] path] type:@"quiz"];

            return YES; 

        } else {
            return NO;
        }
    } else {
        return NO;
    }
}
Run Code Online (Sandbox Code Playgroud)

那是我的readFromFileWrapper:ofType:error:方法.这是我的openProject:type:方法:

-(void)openProject:(NSString *)filepath type:(NSString *)type 
{
    NSLog(@"Opening project @ %@",filepath);
    NSLog(@"%@", [MainWindow description]);
    [projectDesignerView setFrame:[[[[MainWindow contentView] subviews] objectAtIndex:0] frame]];
    [projectDesignerToolbar setFrame:[MainWindow frame] display:FALSE];
    [[MainWindow contentView] replaceSubview:[[[MainWindow contentView] subviews]objectAtIndex:0] with:projectDesignerView];
    [[projectDesignerToolbar toolbar] setShowsBaselineSeparator:YES];
    [MainWindow setToolbar:[projectDesignerToolbar toolbar]];
    [MainWindow setRepresentedFilename:filepath];
    [MainWindow setTitle:[NSString stringWithFormat:@"%@ - %@", [[filepath lastPathComponent] stringByDeletingPathExtension], [projectDesignerToolbar title]]];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"projectDesigner" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:path];
    [[projectDesignerWebview mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
}
Run Code Online (Sandbox Code Playgroud)

NSLog(@"%@", [MainWindow description]);返回nil,MainWindow应该是主应用程序窗口.我认为问题是双击文件会重新分配所有内容,因此失败了.

Rob*_*ger 6

你问的问题并不完全清楚.你提到这MainWindow是一个出口,MainMenu.xib但你没有指定哪个类定义出口.

如果此窗口设计为具有单个主"项目"窗口,则应在应用程序委托中指定outlet属性.

然后,您可以使用类似的东西从其他类访问它[(YourAppDelegate*)[NSApp delegate] mainWindow];.

但是,如果您试图获取对当前文档窗口的引用,那么它会更复杂一些.

默认情况下NSDocument没有window出口的原因是它设计用于NSWindowController自己管理与文档相关的各种窗口的实例.这样一个文档就可以有多个窗口显示相同数据的不同视图,与文档相关的附加调色板等等.每个实例NSWindowController都有自己的窗口nib文件和window插座.

默认情况下,如果您没有专门为文档创建和分配实例NSDocument,NSWindowController则为您创建一个实例NSWindowController.这是自动的,您甚至不需要知道窗口控制器存在.

这意味着,如果您不是NSWindowController自己使用实例管理文档窗口,则可以将窗口附加到NSWindowController自动创建的窗口,NSDocument如下所示:

/* Only implement this in an NSDocument instance where the 
   automatic window controller is being used.
   If the document has multiple window controllers, you must
   keep track of the main window controller yourself
   and return its window
*/
- (NSWindow*)documentWindow
{
    if([[self windowControllers] count] == 1)
    {
        return [[[self windowControllers] firstObject] window]; 
    }
    return nil;
}
Run Code Online (Sandbox Code Playgroud)