我有一个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;
}
}
那就是我的
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]];
}
当
NSLog(@"%@", [MainWindow description]);
应该是主应用程序窗口时,MainWindow
返回nil。我认为问题在于双击文件会重新分配所有文件,因此失败了。 最佳答案
您要问的还不清楚。您提到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;
}
关于macos - 从NSDocument类访问主窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8778809/