• 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: Using a custom NSCell for NSButton created in IB
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using a custom NSCell for NSButton created in IB


  • Subject: Re: Using a custom NSCell for NSButton created in IB
  • From: "Stephen F. Booth" <email@hidden>
  • Date: Mon, 6 Mar 2006 01:19:48 -0800

Hi Greg,

Thanks for your replies- unfortunately I can't get this approach to work either!

I instantiated the CompilationButton class in the nib file, and created a switch (NSButton) and set the custom subclass to CompilationButton. However, I still see the same behavior as before; the button is using NSButtonCell in lieu of CompilationButtonCell.

Stephen

On Mar 5, 2006, at 3:50 PM, Greg Herlihy wrote:

There is one fairly crucial detail that I probably should have mentioned
earlier. In addition to creating the subclass for the NSButton in Interface
builder, it is also necessary to "instantiate" the subclass in IB as well.


Once this is done and the button shows up in IB's nib window, then the
subclassed button control can be created in the window and will work as
expected.


Greg


On 3/5/06 1:30 PM, "email@hidden" <email@hidden> wrote:

I actually tried this before, and it does not work. My NSButton subclass
for some reason still uses NSButtonCell instead of my custom cell class.
(When I print out the type of [self cell], it is NSButtonCell.) I see
that +cellClass is being called, but my custom cell class' getNextValue is
never called.


This was exactly the root of my frustration; it seems that overriding
+cellClass should do the trick and it doesn't. Even the docs allude to
this. As I mentioned, I have a feeling it is because the button object is
being instantiated using initWithCoder, which sets up the object using the
archived NSButtonCell instance instead of creating a new one of the
specified type.


Stephen

If you go the Custom View route than IB won't know that the custom view is
actually a button, as you have found out.


A better idea is to replace the custom view with the standard button that
the custom button inherits from. Then with this button selected, go to the
"Custom Class" pane in the Inspector and set the button's class to your
custom class. In this way, IB will treat your custom button as a button
control. And the easiest way to add your custom class to the list that IB
shows when selecting a custom class, is to open the header file of the
custom button class and then drag the document icon in its window's title
bar onto Interface Builder's main window.


And the only routine that the custom button needs to implement is
+cellClass
(note it's a class - not an instance method). +cellClass just needs to
return the class of your custom cell type.


Greg


On 3/5/06 8:01 AM, "Stephen F. Booth" <email@hidden> wrote:

I recently ran across an annoying problem that I was able to get
around but it seems there should be a better way, so I wanted to ask
here.


I needed to create a subclass of NSButton/NSButtonCell that would act
as a 'pseudo' mixed-mode checkbox- a button where the user can only
select on or off states, but by selecting multiple items elsewhere
the user might cause the checkbox to indicate a mixed state. This is
easily realized by overriding the -(int)getNextState method in
NSButtonCell and telling NSButton to use the new subclass.


My aggravation came in trying to get NSButton to use the custom
NSButtonCell- I could find no way to create a button in IB and have
it use my custom cell class. I ran across a note in the
documentation about calling setCellClass in awakeFromNib; I created a
custom subclass of NSButton and did just that. However, the NSButton
continued to use NSButtonCell instead of my custom subclass,
presumably because the NSButton instantiated in IB was using
NSButtonCell instead of the custom class.


All the custom control examples use custom IB palettes, which
obviously use custom NSCell subclasses; I toyed with the idea of
writing an IB palette but it seemed like a waste of time for such a
simple requirement.

I finally got around the problem by creating a custom view in IB
assigned to a custom NSButton subclass that is configured
programmatically the way I want in awakeFromNib. This approach is
far from ideal because there is no way to modify/preview/alter any of
the numerous NSButton attributes using IB.


Is there an easier way I just missed?

Thanks,
Stephen
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40pacbell.net


This email sent to email@hidden







_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Using a custom NSCell for NSButton created in IB (From: Greg Herlihy <email@hidden>)

  • Prev by Date: Re: Using a custom NSCell for NSButton created in IB
  • Next by Date: error when calling CGImageSourceCreateThumbnailAtIndex
  • Previous by thread: Re: Using a custom NSCell for NSButton created in IB
  • Next by thread: MultipleNibTabView example from Apple
  • Index(es):
    • Date
    • Thread