• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: guidelines/tutorial for implementing custom controls
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: guidelines/tutorial for implementing custom controls


  • Subject: Re: guidelines/tutorial for implementing custom controls
  • From: Rua Haszard Morris <email@hidden>
  • Date: Tue, 5 Aug 2008 14:09:10 +1200

Can anybody point me to some good tutorials/guides for implementing
custom controls?

Do you have some objection to the examples at developer.apple.com ? http://developer.apple.com/samplecode/Clock_Control/index.html http://developer.apple.com/samplecode/TrackBall/ http://developer.apple.com/samplecode/bMoviePalette/index.html

There are some fun third party samples:
http://www.sticksoftware.com/software/CircularSlider.html
http://developer.snowmintcs.com/controls/smdoubleslider/index.html
http://www.stepwise.com/Articles/Technical/NSCell.html

See also Omni Group
http://www.omnigroup.com/developer/

There are some custom controls in the DrawDemo for DrawKit
http://apptree.net/drawkit.htm
Thanks for the links, of those I had only consulted Clock_Control, which seemed too out of date to bother with (it was a .pbproj). I'll have a look at the other links, thanks.

1. How does one setup the target/action mechanism for a custom
control, i.e. client code can call setAction: and setTarget: and have
these stored in the class? ...
Subclasses of NSControl already have target/action support and can be connected via Interface Builder. You can also use NSActionCell without NSControl but won't automatically get Interface Builder interaction. Target action is also easy to implement on your own via NSApplication's - (BOOL)sendAction:(SEL)anAction to:(id)aTarget from:(id)sender method.
I wasn't able to get it to work (from code, not from IB, the target&action connection has to be made from code for my situation); when I called setTarget and setAction in the client code on an instance of my NSControl subclass, and queried the target and action, they were nil/nullselector.

I did work out how to solve this though: implement (override) +cellClass for my custom control class, and return NSActionCell. Then I get target-action for free. This made sense, and indicated I did learn something when reading the docs :)

2. Is wrapping existing controls in a custom control class a good
idea? ....
I guess that would work. Why would you want to do that ?
Well, because it seems that it may be a convenient and standard approach - is it? Or is it flawed and doomed to failure? Or just inelegant?

My (current) higher level goal is implementing a control that includes
3 momentary buttons, the middle button showing a menu when pressed.
...
I don't think you need a custom control to meet any of your goals. Just embed your buttons etc. in a convenient parent view and provide suitable Controller logic (ref: Model View Controller). You can save your embedded views in their own interface builder nib file for reuse or even add your pre-configured group of objects to Interface Builder's library. See "Adding Modified Objects Back to the Library" In Interface Builder's help.

You will need to subclass NSView or one of its existing subclasses when you want custom drawing unless you use CALayer's instead.\
This is a valid point - that I am actually implementing a custom view rather than control. I may in future take this approach for more complex controls/views, but from code rather than in IB (these controls/views are instantiated from cross-platform code).

I've noticed that NSSegmentedControl can implement pretty much all of
the control, so I think I want to use that internally, implementing
all the config for the segmented control, and mapping the actions of
the individual buttons to set the data in the control and invoke the
custom action (that the client code, the target receives).

Is there a way to achieve this? Am I barking up the wrong tree?
I think what you are suggesting is workable. Have you tried it ?
Yep, it was perfect, except for the fact that the appearance I needed was 10.5+ (I need to support 10.4+), and I didn't have a solution for abstracting away the actions for the menu and buttons so the client just saw a unified "value has changed" type action.

What I am looking at now, and it seems appropriate and workable, is subclassing NSControl, setting my class' cellClass to NSActionCell, instantiating and configuring NSButtons, the menu etc appropriately and handling their actions internally, dispatching the custom control's action when appropriate so the client code is notified.

I'm still pondering whether I should be instantiating NSButtons, or NSButtonCells in the NSControl subclass, and if I should be splitting some of that logic into an NSActionCell subclass, but I guess until I have a reason to do so there's no need...

I guess what I'm looking for is a "when to subclass NSView vs. NSControl vs. NSCell" guide, it seems there must be some rules of thumb. I.e. NSView => if you need to draw. NSControl => when you need target/action (remember to implement +cellClass:). NSCell => when you need to reuse your control draw etc logic for NSMatrix, table view etc. And then how to correctly set up the link with your NSControl and NSCell. And then how to make an ib palette....

thanks for the tips
Rua HM.
_______________________________________________

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


References: 
 >Re: guidelines/tutorial for implementing custom controls (From: Erik Buck <email@hidden>)

  • Prev by Date: Re: guidelines/tutorial for implementing custom controls
  • Next by Date: Re: Opening and Saving an RTFD
  • Previous by thread: Re: guidelines/tutorial for implementing custom controls
  • Next by thread: Properties and memory management
  • Index(es):
    • Date
    • Thread