Re: get ref to instance from IB
Re: get ref to instance from IB
- Subject: Re: get ref to instance from IB
- From: Graham Cox <email@hidden>
- Date: Tue, 16 Jun 2009 14:28:37 +1000
On 16/06/2009, at 1:55 PM, Paul M wrote:
First off, I'm new to IB and cocoa, so these questions should be
taken in that context.
Also, This is a port of an existing X/C++ app. The internals are
tried and true, all I'm doing is adding a new UI.
How do I obtain a reference to an object instanciated in IB?
I have 2 outline views and 2 instances of my datasource class, but
I'm not clear how I can contact each instance separately from my
code. I need to set a pointer in the datasource pointing to my
internal (C++) data structures so that the right data goes to the
right view.
I can think of a few ways to do this, but none that are robust or
clean.
What is "File's Owner"? Whatever object that is, add IBOutlets to it
and wire them to the objects of interest in the nib. The outlets are
just ordinary ivars to the object that is "File's Owner".
\On a barely related note -
I thought that the 'item' passed from the datasource to the
outlineview (returned by outlineView:child:ofItem:) should be opaque
to the outlineview, so I was passing a reference to a C++ object.
This however crashed my app whenever the disclosure triangle was
clicked. It seems that the runtime system was monkeying with my
data, expecting it to be am obj-c entity when it wasnt. I got round
it by creating an obj-c wrapper class and wrapping each C++ item
before passing it to the outlineview. This seems horribly
inefficient to me, I have thousands of items in my data trees.
Since item is typed as an id, it must be an Objective-C object. The
outline view may quite reasonably need to retain/release it for
example. It's not "monkeying with your data", you broke your contract
with it by not passing an object of type id.
So you do need a wrapper. But look at NSValue - it can wrap up an
arbitrary pointer, so you don't need to roll your own unless there is
extra functionality that would be worth having in the wrapper.
Also: do I need to track and clean up all these wrapper instanes?
I'm assuming I dont - I hope I dont otherwise it'll be a complete
nightmare.
You need to follow normal, standard memory management and object
ownership rules. That doesn't necessarily involve "tracking" all the
wrapper instances, but it doesn't let you off following the rules
correctly. In this case, autoreleasing the objects when they are
created is probably all you need to do (and +[NSValue
valueWihtPointer:] does this anyway). If you are storing the wrappers
in an array or tree structure built from NSArray and friends, these
will do the right thing, but you'll have to correctly manage the
ownership of these. There's no such thing as a free lunch, but it's
much cheaper than you think ;-)
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden