Quincy thanks for your reply, ok I was going to start sending you tons of little snipits from my code, and I had the whole email, typed but decided to reread your comments carefully.

In a nutshell:

1) I am/was creating my document class "programatically" like: (Note Typed in email, and in my project MyDocument is actually named something else)

MyDocument *newDoc = [[MyDocument alloc] init];
[newDoc importData:data];

By itself this wouldn't pop up a document because of the overridden applicationShouldOpenUntitledFile: method

So I added:

[newDoc makeWindowControllers];  and the method mentioned below.

Reading your notes carefully though it looks like I maybe should be using openUntitledDocumentAndDisplay:error instead of the makeWindowController thing that got my document to popup perhaps only because I added the showWindow:self to the end of that method. And you are probably right that I am getting two instantiations, but only "seeing" one.

I will test your suggestion first, though It might take me a few because the template doesn't generate a NSDocumentController, only the NSPersistentDocument class itself so I first have to figure out how to add the Controller to my AppController class and stuff without breaking everything, I should be able to get openUntitledDocumentAndDisplay:error to work (I bet it does, so here is a pre thank you!!!),

If not I think perhaps I should first deconstruct into a separate clean project so that I can share better examples.

Regards,
Richard






On Dec 31, 2008, at 2:42 PM, Quincey Morris wrote:

On Dec 31, 2008, at 11:58, Richard Ashwell wrote:

4) The next task was based on a selection in the database table, I wanted to programatically open a document and pre populate it from the data.

It's not clear what you mean by "programmatically open a document". Are you invoking the same action method that the Open menu item uses? Telling the NSDocumentController to open a document? Trying to use [[NSDocument alloc] init...]?

It also sounds like perhaps this programmatic "open" actually has the semantics of "new". That is, you're creating a new (untitled) document with some data from your database, which is (eventually) going to get saved in a file separate from your database. If so, calling this "open" is going to confuse us.

Here I ran into the issue that the document from the template was tied to Nib via the Overridden windowNibName: method, which made it difficult to programmatically get the Controller Window when creating a new document from elsewhere in the code. The solution I have here is to comment out the windowNibName method and instead implement:

- (void)makeWindowControllers
{
NSWindowController *mainWindowController = [[NSWindowController alloc] initWithWindowNibName:@"MyDocument" owner:self];
        
        [mainWindowController setShouldCloseDocument:YES];
        [self addWindowController:mainWindowController];
        [mainWindowController showWindow:self];
}

It's not clear how this makes it easier to "get the Controller Window", since the reference to the controller is local to this method. Anyway, once the controller is created, you can always get it as [[document windowControllers] objectAtIndex: 0].

This worked and I could instantiate an instance of my Document Class programatically when ever I want and call method makeWindowControllers on it and the document would come up, more importantly I could then setup the instantiated Document to have its data loaded.

If you really are invoking makeWindowControllers yourself, you probably shouldn't be, since it's normally invoked automatically. Invoking your implementation manually would create a second window controller, which isn't what you want.

To create a new document programmatically, you should probably be invoking [NSDocumentController openUntitledDocumentAndDisplay:error:]. Are you using something else?


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/rashwell%40me.com

This email sent to rashw...@me.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to