Multiple Instances of NSWindow?

Discussion in 'Mac Programming' started by Blakeasd, Sep 7, 2013.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,

    I am trying to create multiple instances of an NSWindow without the use of NSDocument and NSDocumentController. For my purpose, it is overkill. I am using NSWindowController, instead. Here is the code that creates an instance of my NSWindow once, a button is clicked.

    Code:
    
        
        newFileWindowController = [[NewFileWindowController alloc] initWithWindowNibName:@"NewFileWindowController"];
        [newFileWindowController showWindow:nil];
    
    
    When the button is clicked again, no new instances appear.

    The window's nib is connect to the controller via File's Owner. (I just used the standard Xcode 4 new Window Controller template.) Is it possible, though, to make multiple instances of that same window appear when the button is clicked without the use of NSDocument or NSDocumentController. The NSWindowController's documentation is very limited in what it says.

    Thanks!
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    In your snippet it looks like you are just replacing the controller - to have multiple windows you will need to create multiple controllers.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I never know how many times a user would like to que up a new window though. How could I create a new controller every time the user wants to make a new window? I thought that an NSWindowController would be able to create multiple instances of a specific NSWindow. Are you sure that a new controller has to be created?

    Thanks!
     
  4. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    Each time the button is pressed, just add the new controller instance to an array or whatever. Each window controller manages a window.
     
  5. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    Thanks for your response.

    I'm stuck on trying to do this:

    I can't think of a way to dynamically create instances because the instance would require a new name every time it is created. I've looked through the web and the Apple Docs; I can't seem to find a method of creating new instances every time a method is called.

    Could you please provide some insight on how I should do that?

    Thanks for all your help!
     
  6. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #6
    Each time you create a new controller with your alloc/init

    Code:
    [[NewFileWindowController alloc] initWithWindowNibName:@"NewFileWindowController"];
    the instance of the new object is returned. You can do whatever with it, such as put it into an variable (array or whatever), so that you can later use it to refer to that object (e.g. to give the window a name or access whatever else you have placed in that window).
     
  7. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #7
    Ok,

    Here is my new code:

    Code:
       
        NSMutableArray *newFileWindowControllerArray  = [[NSMutableArray alloc]init];
        [newFileWindowControllerArray addObject:[[NewFileWindowController alloc]initWithWindowNibName:@"NewFileWindowController"]];
        
        NSUInteger elementsInArray = [newFileWindowControllerArray count];
        
        NSLog(@"%lu",(unsigned long)elementsInArray);
        
        
        [[newFileWindowControllerArray objectAtIndex:i] showWindow:nil];
    
        
        i++; //Initialized as i =0;
    
    
    For some reason, the window flashes open and then closes very quickly when this method is called. Furthermore, when the button is clicked again I get an exception stating that i > bounds. It turns out that it's not allowing for more than one instance in the array as the number of elements stays at 1 no matter how many times I click.

    I'm not sure what's wrong with my code, to my knowledge it should work.

    Any Ideas?

    Thanks!
     
  8. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #8
    You are creating a new local array each time. Try creating the array in a more global space and add the new instances to that.
     
  9. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009

Share This Page