[Openmcl-devel] object classes and nib files
dankna at accela.net
Sun Jan 2 18:03:24 EST 2005
> I'm having trouble loading an OBJC class from a nib file.
> I have a class HemlockTextView that inherits NSTextView and I do
> ? (make-instance 'ns:ns-window-controller :with-window-nib-name
There is an example of how to do this at the end of:
10.2. Instantiating Objective-C Objects
NSWindowController is something of a special case. It requires the
parameter. The object passed as the :owner should be an allocated but
uninitialized instance of the same class as the one defined in the NIB
the owner. It is then initialized to be identical to the saved object.
Things do appear to work if you leave :owner off, but it's behaviour
This can't be done with (make-instance), because (make-instance) does
both the allocation and the initialization, and it doesn't know about
parameter. Although there's probably no reason it wouldn't work to call
(allocate-instance) and then (initialize-instance), the way that I know
is to first send the 'alloc message to your class, to obtain an
instance, and then send the (:init-with-window-nib-name :owner) message
to that instance.
Here's a working snippet of code from one of my own programs:
(defun init-gui ()
(unless (objc-object-p *student-list-controller*)
(let ((it (send (find-class 'student-list-controller) 'alloc)))
(setq *student-list-controller* it)))
(send *student-list-controller* 'window)
(send *student-list-controller* :show-window nil)))
You don't necessarily need the 'window and :show-window messages, of
You do need the (with-autorelease-pool), unless you've already set up
pool; if you leave it out, nothing will immediately explode, but you'll
Also notice that ObjC initialization methods can return a different
object than the one
you sent the message too (!), so that setq is necessary. Lisp will
if you forget to do that and then try to print-object the result.
> I'm guessing that something in the cocoa runtime code didn't like my
> slot definitions, but I don't know what.
As far as I see, there's nothing wrong with your slot definitions.
> I defined all the slots as Outlets in interface builder, even though
> event-queue, command-thread and
> needs-display are not :foreign-type. I'm only actually connecting
> echo-view and master-view so I'm
> not actually trying to assign into those slots, though.
You shouldn't define event-queue, command-thread, and needs-display
in the nib if they
are not supposed to hold ObjC objects. If they are supposed to be
foreign slots but of no
particular type, you should make them :foreign-type :id. However, this
is not the cause of
your current problem.
-- Dan Knapp
More information about the Openmcl-devel