Search code examples
macoscocoainterface-buildercocoa-bindings

How to bind a property to an external controller?


I work with the SDK 10.6, with Xcode 4.2.

I want to bind a "property" to a controller that exists "out of my nib". How could I do :

  • with Interface Builder ?
  • without it ?

More precisely, I want to bind the enabled property of a NSButton to an arrayController that exists "out of my nib". The only objects to which I can bind properties, in interface builder, are the placeholders or the objects. But :

  • I cannot change the placeholders.
  • If I had an arrayController to my NIB, it will be created when my NIB is loaded.

Why am I in this situation ? Because : the arrayController is in my NIB ; but the button is in another NIB. This second NIB is a subview of my main view.

Thanks for help :)


Solution

  • I think that I found an answer.

    Some notations :

    • MySubiewController and MySubiewController.xib are the couple of objets where I want to perform some bindings. They have to be thought as a subview.
    • Somewhere else in my program, "above", I have already an myMainArrayController. It is an ArrayController, linked with my data, and "created in my main view".

    Now : the answer.

    • First, for instance via the init method of MySubiewController, store your myMainArrayController in some variable (you have to expose this variable in the .h, as a @property (it's better) before using it in your program). Let's give the same name to this variable (you could give any name). For instance :

       - (id)initWithNibName:(NSString *)nibNameOrNil 
                     bundle:(NSBundle *)nibBundleOrNil 
        withArrayController:(NSArrayController *)anArrayController
      {
          NSLog(@"Creation of an instance of MySubiewController");
      
      
          self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
          if (self) {
              myMainArrayController = anArrayController ;
          }
      
          return self;
      }
      

      This is where you will tell to your "subview" who is the arrayController already exisiting.

    • Do your bindings with File's Owner and then self.myMainArrayController.

    • That's it!!! It should work!