I have a NSDocument class, where I'd need to access the main menu window, the one that gets opened when the app start. When I operate in that window from the app all seems to work, but when trying to do the same operations from readFromFileWrapper:ofType:error:
the window I access seems to be nil. Why this happens?
EDIT: Some code which deals with this:
- (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;
}
}
That is my readFromFileWrapper:ofType:error:
method. Here is my openProject:type:
method:
-(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]);
returns nil, when MainWindow
should be the Main App Window. I think the problem is that double-clicking on a file reallocs all, and hence is failing.
It's not entirely clear what you're asking. You mention that
MainWindow
is an outlet inMainMenu.xib
but you don't specify what class is defining the outlet.If this window is designed to have a single main "project" window then you should assign the outlet property in your application delegate.
You can then access this from other classes using something like
[(YourAppDelegate*)[NSApp delegate] mainWindow];
.If, however, you are trying to obtain a reference to the window of the current document then it's a little bit more complicated.
The reason that
NSDocument
does not have awindow
outlet by default is that it is designed to work with instances ofNSWindowController
that themselves manage the various windows related to the document. This is so a document can have multiple windows showing different views of the same data, additional palettes related to the document and so on. Each instance ofNSWindowController
would have its own window nib file andwindow
outlet.By default,
NSDocument
creates a single instance ofNSWindowController
for you if you do not specifically create and assignNSWindowController
instances to the document. This is automatic, you don't need to even know the window controller exists.That means that if you aren't managing your document windows with
NSWindowController
instances yourself, you can get the window attached to theNSWindowController
that is automatically-created byNSDocument
like so: