Re: Custom Control Questions
Re: Custom Control Questions
- Subject: Re: Custom Control Questions
- From: "I. Savant" <email@hidden>
- Date: Thu, 16 Aug 2007 10:12:24 -0400
With respect, it sounds as if you're trying to run before you can
walk. There are a number of things you seem not to understand here, so
I'll try to address each in kind ...
> Until now I've never needed to use the Custom Controls. I've
> found how to create Custom Controls and how to use them in the project from
> the Clock Control application provided by Apple,
You should also have read:
Control and Cell Programming Topics for Cocoa
http://developer.apple.com/documentation/Cocoa/Conceptual/ControlCell/index.html
and
NSControl Class Reference
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html
Read them *thoroughly*. This is required material not just for
creating controls but using the existing ones effectively and
understanding the mechanisms you're using. No amount of help from this
list will come close to doing your own research. Read. The. Docs.
> but the problem is that I
> couldn't find a way to load an image (TIFF) in the Custom View.
If you're just trying to draw an image onto a view, this is so
simple you'll laugh when you see how by reading:
Cocoa Drawing Guide (see Images section)
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/index.html
> I've put in
> the palette the Custom View and the button, and I've made the Custom View of
> the class I needed.
...
> - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
If you're creating a custom view, you typically do not add subviews
to it in Interface Builder. A control (per the documentation
referenced above) uses cells. If you want to use an NSImageCell to
draw the image, that's fine, but your NSControl is responsible for
telling controls to -drawWithFrame:... etc. when its own -drawRect:
method is called. Same for a button cell, a text cell, etc.
The method above is a method of NSCell. Your NSControl is not an
NSCell or a subclass thereof, so why would you expect that method to
be called just because it's present in your class?
>
> [[NSImageCell alloc] initImageCell:[NSImage imageNamed:@"ClockFace"]];
>
> in the init method, the image wasn't displayed.
... why would it be? You've only created an instance of a cell. You
haven't done anything with it at all. Incidentally, if this is the
exact line (in other words, you're not assigning the result to an ivar
in your class), you're leaking that NSImageCell instance (because
you're not maintaining a reference to it and you're not releasing it
before the end of your -init method). Search the docs for "memory
management" and read it thoroughly as well as it's a vital topic
(unless you're targeting Leopard, that is).
Also, -init is not the designated initializer for an NSView (of
which NSControl is a subclass). They should be initialized via
-initWithFrame: ... but since it's being "revived" from a NIB, some
things may need to be done in -awakeFromNib as well ...
> The other question I have, and maybe you can help me, is: Can I use a button
> in a Custom Control?
Of course, but again, you want the NSButtonCell. NSButton is itself
an NSControl subclass which uses NSButtonCell to handle drawing the
button and deal with its target/action, etc. See the docs I referenced
and *do your homework*.
> Should I create a Controller for the actions of the
> button (like in the usual applications) and call those methods from the
> controller in the Control main class (the one that subclasses NSControl)? Or
> is there any other way.
There are many ways ... but essentially your control should be
created so that it can be treated just like any other control (such as
a button, slider, etc.). There's no substitute / shortcut for reading
the documentation here ... this is a very broad topic.
--
I.S.
_______________________________________________
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