bindings between different xib files

Discussion in 'Mac Programming' started by Chirone, Aug 17, 2009.

  1. Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    this is a question about bindings between different xib files

    in the same xib file i can make two windows, one window displays the information of the selected object in an array controller object

    and it works fine, select an item in the table of one window and the information appears in the other

    now... how do i do this when the window is in a different xib file? from what i can tell, the second xib file doesn't know when something in the first xib file is selected..


    also, why can't i use window controller when using bindings? it says its not key value compliant, but what do i use to open the window if i can't use window controller?
     
  2. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    I don't think you can do bindings between different nib files. If you find out how, let us know.

    In your NSDocument subclass, overwrite "makeWindowControllers". Create the two instances of you NSWindowcontroller subclass (one for each window). Make both aware of the other. When the selection in one window (or more precisely in the NSArrayController) changes, propagate the selection change to the other window. Assuming that the data is stored within NSDocument, and the second window can access it. Or you have to channel data back and forth between the two windows (which could take a lot of glue code). Your subclass of NSWindowController will need to overwrite "initWithWindowNibName:".

    Code:
    #pragma mark windowcontroller
    - (void)makeWindowControllers {
    	//custom subclass of windowcontroller
    	MyWindowController *winControl = [[MyWindowController alloc] init];
    	[self addWindowController:winControl];//retain count +1
    	[winControl release];
    }
    -(void) windowControllerDidLoadNib:(NSWindowController *)windowController {
        [super windowControllerDidLoadNib:windowController];
        // user interface preparation code	
    }
    
     
  3. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    i think, then, i've done something totally wrong

    i haven't at any point subclassed the NSWindowController,
    if i use the NSWindowController to open the second nib the console says something along the lines of:
    [<NSWindowController 0x18dbe0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext.
    this is opening the window through the menu (forgot to mention that before but i didn't think it relevant... but it could be...)

    if i run the nib through the method runModalForWindow in the nib's file owner the window shows up, but i get told off for using that method
     
  4. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #4
    That is because in a coredata project, the NSPersistendDocument (and the nib files it automatically opens) is aware of the NSManagedObjectContext since it makes one itself. With the subclass of NSWindowcontroller, you will have to add an instance of NSManagedObjectContext to your nib file. This you bind to an outlet of your NSWindowcontroller.
    (untested code)
    Code:
    NSManagedObjectContext *managedObjectContext; // in the header file
    @property (readonly,assign) managedObjectContext;
    
    -(NSManagedObjectContext *) managedObjectContext {
     return [[self document] managedObjectContext];
    }
    
    
    I also think you are better of giving each window its own subclass of nswindowcontroller. This way you separate the data in NSDocument from the visual representation in the nswindowcontroller's. For anything other than small projects, you would want to do it this way.

    Well, you better listen then. :)
    Why are you getting told off?
     
  5. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #5
    i'll try to give that a go... do you have an example of what you mention?

    it tells me off saying:

     
  6. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #6
    I think the problem is that you want a modal window.
    Add this code in you subclass of nswindowcontroller and try to "init" it in the makewindowcontrollers, as I showed earlier. That should open a regular window.

    Code:
    #pragma mark init
    
    - (id) initWithWindowNibName:(NSString *) value {
    	//see documentation.
    	NSLog(@"Don t use initWithWindowNibName: %@",value);
    	return nil;
    }
    - (id) init {
    	self = [super initWithWindowNibName:@"MyDocument"];
    	if (self != nil) {			
    	}
    	return self;
    }
    
    As to making it modal, Don't you need a NSPanel for that, instead of a regular NSWindow?
    Is it one secondary window per document, or for the entire application?
     
  7. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #7
    right well, i get what you mean when you tell me to subclass the nswindow controller to open new windows

    trudging through it all now... we'll see how it turns out hopefully soon
     

Share This Page