Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > custom NSButton class

custom NSButton class
Thread Tools
arcticmac
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
May 6, 2004, 10:10 PM
 
I want to create a custom class of NSButton, that I can use on buttons in a nib file(duh) that will set some default things about the button (title, style, as well as some other special attributes for my subclass) when it is loaded, but I can't get it to work.

my question is, what method of NSButton do I need to override to have it do my special initialization stuff?
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
May 7, 2004, 02:08 AM
 
The designated initializer for NSControls is initWithFrame:(NSRect)frameRect. You should be putting your code there.

I've had a problem in the past with using subclassed controls in IB. If I drag an NSButton (for example) to my nib and set the custom class, that will not call my initWithFrame. But if I drag a CustomView to the nib and then set the custom class, that will call initWithFrame. I presume this has to do with the way controls are unpacked from a nib, but I don't really know.

Of course, you can always use awakeFromNib. It's cheezy, but it always works.
     
Uncle Skeleton
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
May 7, 2004, 02:07 PM
 
wouldn't all -initWithSomething methods eventually lead back to -init, so you could put your initialization code in -init?
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
May 7, 2004, 02:18 PM
 
Originally posted by Uncle Skeleton:
wouldn't all -initWithSomething methods eventually lead back to -init
Actually, no. In fact, it will usually be the other way around. A good discussion of this topic (designated initializers) is at developer.apple.com.
     
arcticmac  (op)
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
May 7, 2004, 05:32 PM
 
OK, that works, but now it doesn't look like a standard push button, and I'd really prefer that it did... any suggestions?

edit: hm... just read your posts (I hadn't reloaded the browser since 6 hrs ago). at first sight, I would have expected all the init methods to come back to init, but when you think about it, there are many calls that essentially do the same thing, and it kinda makes sense anyway.
( Last edited by arcticmac; May 7, 2004 at 05:40 PM. )
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 7, 2004, 05:56 PM
 
Most controls are initialized via -initWithCoder: when being unpacked from the nib. If you set up a custom control by putting an NSButton in the nib and setting a custom class on it in IB, that's the method you'll need to override. But since -initWithFrame: is still officially the designated initializer, you'll want to override both (so that you're compatible with potential future changes to AppKit, and so that other developers using your code can expect it to work as documented).

If you look at the OmniAppKit source code, you'll notice a common pattern in many of our widget classes: we override both -initWithFrame: and -initWithCoder:, and both just call super and a -_commonInit (or similarly named method)... that way we don't have the initialization code duplicated in two places.
Rick Roe
icons.cx | weblog
     
malcolmj
Fresh-Faced Recruit
Join Date: Apr 2004
Location: Ontario, Canada
Status: Offline
Reply With Quote
May 12, 2004, 12:33 AM
 
Originally posted by arcticmac:
I want to create a custom class of NSButton, that I can use on buttons in a nib file(duh) that will set some default things about the button (title, style, as well as some other special attributes for my subclass) when it is loaded, but I can't get it to work.

my question is, what method of NSButton do I need to override to have it do my special initialization stuff?
In the nib mac a subclass of NSButton. Then click on the button in the nib, and click "Get Info" to look in the property inspector. Click the drop down menu to custom class, and select the subclass of NSButton.

Create the file for the subclass and put all of your overriding stuff in there. It might be initWithRect: but you'll have to check the header file for NSButton.

Try that, I may be wrong but meh.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:40 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,