Re: an interesting delegate design issue raised by IB...
Re: an interesting delegate design issue raised by IB...
- Subject: Re: an interesting delegate design issue raised by IB...
- From: Enrique Zamudio <email@hidden>
- Date: Wed, 12 Sep 2001 00:59:31 -0500
- Organization: Nasoft
I think you're getting a little confused here. What you're trying to do
here is not a delegate but rather a data source.
A delegate is an object (any object) that responds to certain methods
that your object defines, but the idea is that if your object has a
delegate, it has to do two things:
1) ask for permission to do stuff, and
2) notify the delegate when the stuff has been done.
A delegate can implement only the methods that interest it; if your
delegate does not implement a method that you need to get permission to
do something, then you just go ahead and do it. If your delegate does
not implement a method that lets it know you've done something, then you
just don't send that message.
A data source is an object that conforms to a more formal protocol
(although the methods can be defined in an informal protocol). It has to
respond to at least some of the methods that you define for your data
source, so that you can get the objects you need.
Check out NSTableView, for example. It has both a delegate and a data
source. It needs the data source to get the rows and columns it will
display, and (optionally) to edit the values it's displaying. It uses
the delegate to ask for permission to change the selection, reorder
columns, edit a certain value, etc. and notify it when those events
happen. That's why you see methods like "tableView:shouldDoWhatever:"
and "tableView:didSomething:"; one usually returns a BOOL and the other
one is usually void. The methods receive the sender as a parameter
because many objects can have the same delegate.
I won't get into the details of the IB implementation you're trying to
do, however I would like to point out that storing images inside a nib
is not recommended anymore. Images should be stored as resources in your
project (whether it's a framework or an application). So you should be
careful when your user is adding images to the data source; perhaps you
should dig deeper into the IB framework to check if there's a way you
can pop up a panel asking the user if they want to add that image to the
project in case it's not already there (wouldn't that be pretty cool?)
but that means that you will communicate with PB from within IB.
As for the data source, you can create an abstract class of data source
for your image view, so that you define the methods you need; or you can
define a formal protocol and expect your data source to conform to that
protocol. And then you can write your own simple data source that works
from IB like you were describing.
As you know, abstract classes in ObjC don't really exist, so you
artificially define them, by raising an exception in all the methods
that you want to force your users to override (that's how I remember
that EODataSource was implemented in ObjC EOF).
Hope this helps a little,
eZL
Enrique Zamudio Lopez
Chief Software Architect
North American Software, S. A. de C. V.
Xalapa, Ver. (Mexico)