1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

optimum design for document template picker?

Discussion in 'Mac Programming' started by MrFusion, Jan 31, 2009.

  1. macrumors 6502a

    #1
    I want to be able to pick a document template out of a list. This is a quick action, and the helping window doesn't have to stick around for long.

    How do I have to deal with the release call in a nice and clean design? Here, the release call is called before the user makes a choice.

    Thanks.

    Code:
    -(IBAction) newDocument:(id)sender {
     WinController *wc = [[WinController alloc] init];
     [wc setoptions:options];
     [wc setselector:@selector(createDocumentFromTemplate:)];
     [wc showwindow:nil];
     [wc release];
    
    }
    
    -(void) createDocumentFromTemplate:(NSString *) template {
     //add document
    }
    
    
     
  2. Moderator emeritus

    kainjow

    #2
    If the templates are actual documents that are managed by your NSDocument subclass, I would do it outside of the NSDocument class and in your own separate controller object. Then in the controller, use NSDocumentController to create a new document based on the chosen template. I did something like this a few years back and this is the general model I used.
     
  3. macrumors 6502a

    #3
    Thanks for the advice.

    Can I also not somehow do it in a subclass of NSDocumentController? The code I posted is sitting in such a subclass.

    I'll play around with your suggestion. Thanks.
     
  4. macrumors 6502a

    #4
    I found a solution, if anyone is interested.

    In MyDocumentController, newDocument is overwritten and contains the code below (not all is shown).
    The window has a cancel and ok button.

    Code:
    TemplateWindowController *twc = [[TemplateWindowController alloc] init];
    if ([NSApp runModalForWindow:[twc window]])
    	newDoc = [self makeUntitledDocumentFromTemplate:[twc valueForKey:@"chosenTemplate"] error:&err];
    [twc release];
    
    Code:
    -(IBAction) cancelTemplate:(id)sender {
    	[NSApp stopModalWithCode:0];
    	[self close];
    }
    -(IBAction) chooseTemplate:(id)sender {
            chosenTemplate = ...
    
    	[NSApp stopModalWithCode:1];
    	[self close];
    }
    
     

Share This Page