Using Protocols (complete message this time...)
Using Protocols (complete message this time...)
- Subject: Using Protocols (complete message this time...)
- From: mw <email@hidden>
- Date: Mon, 30 Dec 2002 09:42:27 -0500
(Sorry about the repost. My last one wasn't complete since a certain
possessed Microsoft program decided to send the message without me telling
it to... *coughEntouragecough*. The completed message follows).
Hello,
I am creating a class for a custom control (a button with rollover states).
This class needs a "helper" class, or a delegate, so I decided to create a
formal protocol that defines all of the functions this delegate class needs
to have so the rollover button can get things like its rollover states and
selectors that should be performed on the different states.
So here is the interface for the custom class:
@interface NPRolloverButton : NSImageView {
NSImage *normalState;
NSImage *overState;
NSImage *downState;
id <NPRolloverProtocol> controller;
SEL downEvent;
}
As you can see, the "controller" can really be of any class, as long as it
conforms to the protocol NPRolloverProtocol (which contains 5 or 6
functions).
Here is where the problem comes in.
The custom button is a subclass of NSImageView (for easy image drawing)
which is placed in the nib file. The controller is not in the nib file at
all. All of the initialization of the custom button is done in its
-awakeFromNib method, which means that the controller object won't be
created yet (since non-nib objects are created after nib-based objects are).
So I want to call alloc and init on 'controller'. This is the first problem.
How the heck do I allocate and initialize a dynamically typed object? I have
no idea what the class is, and I have tried using things like [controller
class] in the alloc-init statement (formed like [[[controller class] alloc]
init];), but that doesn't work at all. The program still compiles, but I get
a warning that says that -class isn't implemented by the protocol, and then
when that line is executed... nothing happens.
A second problem is that whenever I call ANY method on the controller (other
than a method that is part of the protocol, such as -performSelector), the
compiler issues a warning that says that -performSelector isn't part of the
protocol. But since -performSelector is a function of NSObject, it is a
given that all classes will be able to perform -performSelector. Will this
prevent the selector (for the down state) from being performed, or is it
just a precautionary warning?
TIA,
Mw
P.S. If you can't already tell, this is my first time ever working with
custom protocols. I am just used to built-in protocols like NSCopying,
NSCoding, etc. Please be patient with me :-)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.