How to use radio buttons???

I found out this week that I cannot make a radio group anymore ever since Xcode 7.2 did away with Radio Group and NSMatrix. So it seems that I can't use Radio Buttons at all. The documentation that "explains" it doesn't really explain anything. Something about a view something or other. Can anyone enlighten me on this? I admit I'm a bit desperate because the rest of the internet and a week's worth of searching didn't turn up anything useful. Thanks in advance for any help with this.

Replies

You don't need a group or matrix any more. Just put the radio buttons inside the same view, and give them the same action. From:


https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKitOlderNotes/


"To have the button work in a radio group, use the same -action for each NSButton instance, and have the same superview for each button. When these conditions are met, checking one button (by changing the -state to 1), will uncheck all other buttons (by setting their -state to 0)."

Any example of a "superview"? I'm looking through the stuff in the link you provided. Thanks for trying to help! Much obliged!

All views are in a single hierarchy (tree structure) where each view ("child") has a parent view ("superview").


If you're using a storyboard, your scene has a single top-level view where you can add other views such as buttons, text fields and so on. So just drag multiple radio buttons into your storyboard scene, line them up how you want them to look, change their text so they say what they do, and give them all the same action method.


If your view hierarchy is more complicated for some reason, just put all the radio buttons in the area where you would have put the matrix or group.

Well, the example I was working from used a NSMatrix group for the radio buttons and then the @IBAction method used the radioMatrix.selectedTag() as a way to determine which radio button was clicked. What would replace the NSMatrix? The main window variable? What I'm doing is a totally barebones example, and I do mean totally barebones. Just a basic window with some radio buttons and a push button that calls up an alert to tell me which button was selected. It's just for illustration purposes. It was NOT using the storyboards mode and it was for an OS X application using Swift.

Presumably radioMatrix.selectedTag() returned the tag of the radio button that was clicked. Without the NSMatrix, your radio buttons still have tags (they're generally configured in Interface Builder, or perhaps programmatically, depending on what tutorial you're following).


So, use the 'sender' parameter to the action method. Check via 'as?' that it's a radio button, then get the radio button's 'tag' property, and then you can resume following the logic path that you already have.

I did try them in the same window and the build succeeded, but no alerts popped up. What happened was that I was able to select every radio button instead of having them behave in the usual manner. Not sure what I did wrong. Below is the code, it's very barebones simple, nothing fancy at all. Just enough to try to get it to work so I could get the hang of how radio buttons work:


import Cocoa


@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var zeroRadioButton: NSButton!
    @IBOutlet weak var oneRadioButton: NSButton!
    @IBOutlet weak var twoRadioButton: NSButton!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        /
    }
    func applicationWillTerminate(aNotification: NSNotification) {
        /
    }

    @IBAction func whichRadioButton(sender: NSButton) {      
       
        let myAlert = NSAlert()
       
        if zeroRadioButton.selectedTag() == 1 {
            myAlert.messageText = "You clicked the radio button \(zeroRadioButton.stringValue)"
            myAlert.runModal()
        }
       
        if oneRadioButton.selectedTag() == 1 {
            myAlert.messageText = "You clicked the radio button \(oneRadioButton.stringValue)"
            myAlert.runModal()
        }
       
        if twoRadioButton.selectedTag() == 1 {
            myAlert.messageText = "You clicked the radio button \(twoRadioButton.stringValue)"
            myAlert.runModal()
        }
       
    } /
}

You want 'tag', not 'selectedTag'. The latter is for composite controls (like NSMatrix) that report a tag from one of their components.

Actually, what finally helped me was using the name of the radio button and determining its state as in "whateverRadioButton.state" and then using if-else statements to find which one had the "on" or "1" state and from there, use "whateverRadioButton.title" to get its string value for the alert message. Now all I have to do is figure out how to pass along which radio button is selected and have it so the message doesn't pop up until the push button is clicked. As it stands now, the alert message pops up as soon as I change which radio button is selected. But at least the choices are exclusive and it accurately tells me which one has the "on" state. So ... progress however slow! Thanks for your help with this, Quincey!

The revised code is as follows:


import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var zeroRadioButton: NSButton!
    @IBOutlet weak var oneRadioButton: NSButton!
    @IBOutlet weak var twoRadioButton: NSButton!
    @IBOutlet weak var radioMessageField: NSTextField!
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        /
    }
    func applicationWillTerminate(aNotification: NSNotification) {
        /
    }
    @IBAction func pushButtonAction(sender: AnyObject) {
      
        let myAlert = NSAlert()
        var messageText : String = ""
      
        if zeroRadioButton.state == 1 {
            messageText = "You selected radio button \(zeroRadioButton.title)"
        } else if oneRadioButton.state == 1 {
            messageText = "You seleted radio button \(oneRadioButton.title)"
        } else if twoRadioButton.state == 1 {
            messageText = "You selected radio button \(twoRadioButton.title)"
        } else {
            messageText = "Nothing selected"
        }
      
        myAlert.messageText = messageText
        myAlert.runModal()
    } 
}