Radio Buttons in Swift without NSMatrix

Radio Buttons

Radio Buttons using NSMatrix has been deprecated and information on how to use Radio Buttons without NSMatrix is difficult to find. So this is documentation of a way to use the new Radios Buttons using Xcode 7.2.1 and Swift 2.1.1.

Create a new Xcode project and choose Cocoa Application under OS X Application. Call it Radio Buttons and check Swift. Do not check Use Storyboards, Create Document-Based Application or Use Core Data.

Start by creating a new view controller. In the navigator in the left sidebar highlight the yellow folder labelled Radio Buttons. In the menubar File -> New -> File. Select OS X -> Source -> Cocoa Class. Use MasterViewController for the class. Select NSViewController to subclass from. Check the XIB file and the language should be Swift.

Select the MasterViewController.xib in the navigator in the left sidebar. At the bottom of the right sidebar find Radio Button and drag three of them onto the canvas. Using the mouse drag over all three radio buttons and then group them by going to Editor -> Embed in -> Box. Double click on Box and rename it to Radio. Repeat for the three radio buttons with Internet, Broadcast and Underground. Resize the elements.

We need to create a property for the Master View Controller so the display is retained. In AppDelegate.swift right under the class declaration add the line

var masterViewController: MasterViewController!

Now we have to create the view when the application is launched and assign it to the property. In applicationDidFinishLaunching() add

masterViewController = MasterViewController(nibName: "MasterViewController", bundle: nil)
window.contentView!.addSubview(masterViewController.view)
masterViewController.view.frame = (window.contentView! as NSView).bounds

The first line loads the view from the nib file (generated from the xib file). The default view for the default window is contentView. So the second line adds our new view as a subview for the default view. The third line locates the subview in the upper left corner of the window.

Run the application and now the window should be loading. Try clicking the buttons and notice that more than one can be selected. The description of the Radio Button is “For a single choice among mutually-exclusive options.” So they are not working as expected.  We still have some more work to do.

Select MasterViewController.xib and click the overlapping circles in the upper right corner (Assistant Editor) to open MasterViewController.swift. In the Object listing expand until the controls for the three radio buttons appear. Drag from one of them to the bottom of the MasterViewController class. Insert an action called setRadios. Drag from the other two and also connect them to the setRadios action.

<@IBAction func setRadios(sender: NSButton) {
}

Now run the application and try selecting radio buttons. Now we get the expected behavior just by adding an empty action.

The next step is to set the default selection. For this drag from each of the three radio button controls to the top of the MasterViewController class and name them internet, broadcast and underground.

@IBOutlet weak var internet: NSButton!
@IBOutlet weak var broadcast: NSButton!
@IBOutlet weak var underground: NSButton!

Now in the viewDidLoad function add the line internet.state = 1

override func viewDidLoad() {
    super.viewDidLoad()
    internet.state = 1
}

Run the application and now the Internet radio button should be selected by default.

The last step is to retrieve the selection for the radios. At the top of the MasterViewController class add a variable

var radioSelected: String!

At the end of viewDidLoad set the value. We add a print statement to demonstrate that the selected radio can be retrieved.

radioSelected = internet.title
print(radioSelected)

Now in the setRadios function add the lines

radioSelected = sender.selectedCell()!.title
print(radioSelected)

Run the application and you should be able to see the state of the radio buttons appear in the console.

Categories