Search code examples
swiftnsviewnstextfieldnsbutton

How to add radio button property to Custom View using NSButton and NSTextfield


I m creating a custom NSView, it has NSBUTTON of type radio and NSTextfield. Right now, it doesnot show property of radio button i.e. if I select one radio button, other radio buttons should go off. Any fix

the layer of views is like this NSCUSTOMBUTTON has NSTEXTFIELD AND NSBUTTON, THEY are then added to the stackview, which is further added to the super view.


Solution

  • Edit: I found the answer, basically you have to implement your own delegate function, that handles mousedown event and then add the functionality of radio button where you extend the delegate.

    private func handleClickAction(_ clickedButton: ChoiceButton) {
            guard isRadioGroup else { return }
            if previousButton?.value != clickedButton.value {
                previousButton?.state = .off
                previousButton = clickedButton
            }
        }