|
|
custom NSButton class
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2004
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status:
Offline
|
|
wouldn't all -initWithSomething methods eventually lead back to -init, so you could put your initialization code in -init?
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2004
Status:
Offline
|
|
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.
)
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Apr 2004
Location: Ontario, Canada
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|