Re: [Pyobjc-dev] Re: Multiple Nibs
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2004-05-08 06:24:44
|
On 8-mei-04, at 7:58, Winston Wolff wrote: > That's one thing I don't understand. In Obj C, I would call [NSBundle > loadNibNamed: "myModelPanel owner: self] and this will connect 'self' > to the outlets and actions for the nib. I.e. 'self' is the owner of > the NIB. But with Python, you don't specify any owner. How do you > choose which object gets to be the owner of the NIB file? > > For example, if I have: > NibClassBuilder.extractClasses("MyModalPanel") > myModal = MyModalPanel.alloc().init() That won't work :-) NibClassBuilder.extractClasses is a function that extracts class definitions from NIB files. That information is *only* used by NibClassBuilder.AutoBaseClass. > > Now MyModelPanel is a NIB file which specifies a > MyModelPanelController object. So when the NIB file is unfrozen, it > will create an instance of MyModelPanelController. First of all, > where exactly is the NIB file unfrozen? Is it in extractClasses() or > in MyModelPanel.alloc().init()? Second, how do I get a reference to > MyModelPanelController? Neither, it is unfrozen by NSBundle.loadNibNamed_owner_ > Normally I would say you attach it to an outlet of the NIB File's > Owner, but where do I get a reference to the Owner? > > Does this make any sense? A slightly longer explanation... You can define classes in Interface Builder (Classes -> Subclass <SomeClass>). Doesn't doesn't really define a class, but creates a note in the NIB file that the program that will load the NIB will contain a definition of the class. E.g. you promise IB that you will implement the class in your program. IB can create empty Objective-C implementation files for the classes you "define" in IB, but that will overwrite any existing implementation files for those classes. These are not usefull for python anyway (wrong language). You can use NibClassBuilder as a script and then it will behave the same as 'Classes -> Create Files for <SomeClass>' in Interface Builder. This is useful for creating the initial (empty) implementation of classes, but cannot be used when you change the class in Interface Builder (add another action or outlet) because the script will always create an empty implementation (just like the Create Files command of IB). If you add a new outlet to the class you must therefore manually add this to your class definition. This is where NibClassBuilder.AutoBaseClass and NibClassBuilder.extractClasses come into play. These two are used to extract information about the classes you declared in IB from the NIB file and automaticly add outlet definitions to your class. You must still add actions to your class, but you would have to do that anyway because the NIB doesn't contain imformation about the behaviour of the action. Say to define MyController inside IB, with outlet myWindow and action myAction:. NibClassBuilder.extractClasses("MyNib") class MyController (NibClassBuilder.AutoBaseClass): def myAction_(self, sender): print self.myWindow NibClassBuilder.AutoBaseClass is a magic base class that will look up 'MyController' in the information extracted from the NIB file(s). It will then replace itself by the actual base class (as defined in the NIB) and add an outlet definition for the outlets. You could also have written: class MyController (NSObject): myWindow = objc.IBOutlet("myWindow") def myAction_(self, sender): print self.myWindow I hope this helps you understand what is going on, Ronald |