Re: User input while reading document
Re: User input while reading document
- Subject: Re: User input while reading document
- From: "Kyle Sluder" <email@hidden>
- Date: Mon, 8 Sep 2008 00:33:34 -0400
On Sun, Sep 7, 2008 at 11:28 PM, Randy Widell <email@hidden> wrote:
> Is this a fundamentally sound design? Is there a better way to do this?
Something like the following (WARNING, untested code written in Compose window):
-- BEGIN CODE --
/////
// MyDocument.h
@interface MyDocument : NSDocument
{
IBOutlet NSWindow *configWindow;
}
// I'm assuming here that you'll only want to do this once, when the
document is loaded.
- (void)showConfigOptions;
- (IBAction)okButtonClicked:(id)sender;
/////
// MyDocument.m
@implementation MyDocument : NSDocument
- (void)showConfigOptions
{
// This is a striking example of when an abstract base class might
be more appropriate.
// We're going to load the appropriate nib based on our document
type. The File's Owner
// class is set to MyDocument, and the configWindow outlet is bound
to the sheet window.
if([[self fileType] isEqualToString:@"SmallDocFileType"])
[NSBundle loadNibNamed:@"SmallDocConfigOptions" owner:self];
else
[NSBundle loadNibNamed:@"BigDocConfigOptions" owner:self];
[NSApp beginSheet:configWindow modalForWindow:[[[self
windowControllers] objectAtIndex:0] window]
modalDelegate:self
didEndSelector:@selector(configOptionsDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)okButtonClicked:(id)sender
{
[NSApp endSheet:configWindow]
}
- (void)configOptionsDidEnd:(NSWindow *)sheet
returnCode:(NSInteger)code contextInfo:(void *)ctx
{
[sheet orderOut];
// Here, you might determine if the options were sufficiently set,
and close the document if not.
}
@end
/////
// MyDocumentController.m
@implementation MyDocumentController
- (id)openDocumentWithContentsOfURL:(NSURL *)url display:(BOOL)display
error:(NSError **)error
{
if(!display)
// You'll need to do the appropriate thing here to handle when the
document shouldn't be displayed.
;
NSDocument *theDocument = (NSDocument *)[super
openDocumentWithContentsOfURL:url display:display error:error];
if(!theDocument)
return nil;
[theDocument showConfigOptions];
}
- (id)openUntitledDocumentAndDisplay:(BOOL)display error:(NSError **)error
{
if(!display)
// Same as above.
;
NSDocument *theDocument = (NSDocument *)[super
openUntitledDocumentAndDisplay:display error:error];
if(!theDocument)
return nil;
[theDocument showConfigOptions];
}
-- END CODE --
There are a bunch of variations that could be made on this (for
example, failing to properly specify options might cause the document
to close, which can be bubbled up to the NSDocumentController
subclass). Don't forget that you'll need to register your
NSDocumentController subclass -- typically, your NSApplication
delegate will just alloc and init an instance of MyDocumentController,
which will become the singleton document controller for your app.
Hope that helps,
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden