PDA

View Full Version : Subclassing NSButton




Blakeasd
Sep 27, 2010, 05:04 PM
Hello!
Does anyone know of a good tutorial on making custom buttons by subclassing them? I have looked all over and have not found one that has been helpful. Please do not link me to any Apple Documentation.
Thanks,
-Blake



foidulus
Sep 27, 2010, 05:38 PM
Hello!
Does anyone know of a good tutorial on making custom buttons by subclassing them? I have looked all over and have not found one that has been helpful. Please do not link me to any Apple Documentation.
Thanks,
-Blake

It would be helpful to know what you are trying to do with it, and why you cannot use delegates to get what you need. In Cocoa/Obj-C you don't really subclass things all that often, esp. when compared to languages like Java.

chown33
Sep 27, 2010, 05:40 PM
A custom NSButton is usually done by subclassing NSButtonCell, not NSButton.

Maybe you should search for examples subclassing NSButtonCell.
http://www.cocoadev.com/index.pl?NSButtonCell
http://www.cocoadev.com/index.pl?SubclassingNSButtonCell

Or maybe you have something in mind that requires subclassing NSButton instead of NSButtonCell. In that case, please describe why you need to subclass NSButton instead of NSButtonCell.

Blakeasd
Sep 28, 2010, 03:44 PM
I want to try to make iPhone type buttons(Just how they look). I know that doesn't follow human-interface guidelines, but I need it for something I am doing. Anyway please respond and thanks :)
-Blake

foidulus
Sep 28, 2010, 09:37 PM
I want to try to make iPhone type buttons(Just how they look). I know that doesn't follow human-interface guidelines, but I need it for something I am doing. Anyway please respond and thanks :)
-Blake

https://sites.google.com/site/randomprogrammingsamples/iPhoneBtn.tgz?attredirects=0&d=1

This should get you started, you will obviously want a better image(probl with transparency as well), I just took a quick screenshot of a button from the iPhone simulator.

The basics is that you have to subclass NSButtonCell and override a few of NSCells methods(namely the initialization and drawing methods). You also have to make sure to keep everything sized appropriately, this code does that.

You also have to make sure you change the class of the button's CELL in IB, you can also just copy/paste the button once you have one setup.

Hope this helps.

kainjow
Sep 28, 2010, 09:56 PM
^ missing image ;)

253843

foidulus
Sep 29, 2010, 03:25 AM
^ missing image ;)

253843

Oops, forgot to actually have xcode copy the image when I imported it!

Fixed, original link should now point to the new version!

Blakeasd
Sep 29, 2010, 03:25 PM
This is a stupid question, but how exactly to I drag a NSButtonCell to my window. It just drags back to the library?

Sydde
Sep 29, 2010, 04:17 PM
This is a stupid question, but how exactly to I drag a NSButtonCell to my window. It just drags back to the library?

Switch your nib window (the window that shows the nib contents) to list view. From there you can expand items until you see your button. Select the cell (which you get to by expanding the button) and set the cell's class in the info pane of the inspector panel.

kainjow
Sep 29, 2010, 10:02 PM
You can also create a custom button appearance without subclassing. Just create the on and off images, then create your button in Interface Builder as a Square Button. Add the images to your project, and set them as the Image and Alt. Image on the button. Set the Border to off, and change the Type to Momentary Change.

Reasons to subclass would be: nice appearance no matter the size of the button (can also be done with vector graphics too, e.g. PDF), mouse-over effects, and custom text styling.

Which of the iPhone buttons are you trying to reproduce? Can you provide a screenshot?