CocoaDev

Edit AllPages

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/index.html

An AppKit class, controlling an NSButtonCell, and acting as the buttons you see just about anywhere you see them.


Here are some different implementations of NSButton, all taken from Apple’s “Buttons” conceptual topic. Note that the last two, while possibly three buttons on top of each other, are each more likely an NSMatrix of three instances of NSButtonCell.

http://developer.apple.com/documentation/Cocoa/Conceptual/Button/Concepts/art/buttonpush.gif

http://developer.apple.com/documentation/Cocoa/Conceptual/Button/Concepts/art/buttonicon.gif

http://developer.apple.com/documentation/Cocoa/Conceptual/Button/Concepts/art/buttonsradioon.gif

http://developer.apple.com/documentation/Cocoa/Conceptual/Button/Concepts/art/buttonswitchmixed.gif


Creating an instance programmatically

I made a subclass of NSButton and added some custom functions to it. Now I want to display one or more instances of this class on screen with specific attributes, like width, height, highlight state, continuousness, etc…

I want to display the control programmatically, because at run time, there will be an undetermined number of buttons (my custom class) that will be created. I want them to be allocated, initialized, and displayed at certain coordinates, with certain size, etc…


The following code is an example of how to create a button programatically. Just replace My_Button with the name of your subclass and remember to #import the header. – Bo

The frame parameter can be created using NSMakeRect( x, y, width, height) and specifies both the size and location of your button.


Create a Matrix of Buttons Programmatically

In response to a previous request on Apple’s Cocoa-dev list for a sample of how to create a matrix of radio buttons programmatically, I just wrote the following little sample with commented source code in an Xcode project. There are countless little samples out there already. Here is one more with my spin on the techniques.

Thanks to Scott Anguish and Stepwise for hosting this sample: http://graphics.stepwise.com/EMB/RadioSample.zip

This sample creates four radio button matrices using four different techniques:

Note: There is almost never a reason to use techniques 3 and 4

Note: Technique 2 is used by IB whenever entering “Run” mode and a similar technique is used when any nib is loaded in any application.

Note: There is virtually nothing that IB can do that you can’t do in code because every user interaction within IB ends up calling code similar to the code in techniques 3 and 4. IB executes the code to configure a “live” object. When you save a nib file, the live objects with all of their configuration (state) are archived. When a nib is loaded, the objects are unarchived and come alive again. http://www.stepwise.com/Articles/Technical/FreezeDriedObjects.html IB exists so that you don’t have to write or maintain all the code in techniques 3 and 4. At least use technique 2 in favor of techniques 3 and 4.

The approaches used for a matrix of radio buttons in the example can be used with any Objective-C objects based on NSObject and supporting the NSCoding protocol. This includes all Cocoa controls and can include any objects you create.

Here is the relevant code from the example in case you don’t want to download the whole xcode project.

// Technique 2

// Technique 3

// Technique 4