How to Determine if NSPopUpButton Has Changed?

How can an app detect when a NSPopUpButton has NOT had an item selected (popup button changed)?


Normally I use a Save button to begin the completion of all input field edits. At this point, how can I test to insure an item in a popup button has been selected?


In Microsoft's Visual Studio, the index for the popup is equal to -1 until a selection is made. At that thime, the index is changed to show the item selected in the popup. So, a -popup index of -1 tells the app no selection has been made from the popup.

Answered by goldsdad in 213824022

viewDidLoad() could store the initially selected item of the pop-up in a property of the view controller, then you could later test to see if there has been a change of selection.


class ViewController: NSViewController {

    @IBOutlet weak var popUp: NSPopUpButton!
  
    private(set) var popUpInitiallySelectedItem: NSMenuItem?
  
    override func viewDidLoad() {
        super.viewDidLoad()
        popUpInitiallySelectedItem = popUp.selectedItem
    }

    @IBAction func popUpSelectionDidChange(_ sender: NSPopUpButton) {
        if sender.selectedItem === popUpInitiallySelectedItem {
            print("User has re-selected the initially selected item.")
        } else {
            print("User has selected some item other than the initially selected item.")
        }
    }
}

why not disable the button when opening the view and enable it in the func that handles the pop up ?

Claude, this doesn't answer the question, Has the popup been selected? There are a number of things I could do, such as reverse a boolean flag, to show a choice was selected, but that doesn't answer my question, "How can I tell later in the applicable ViewController.swift, that the PopUp's index has chanaged from its initial load?


By the way, under View Did Load, the PopUp is populated. As I mentioned in the original post, Microsoft's Visual Studio lets you set the popup's index to -1. Checking the index for -1 lets you know no selection has been made. In Xcode/Swift, there has to be a way to make the same distinction. Right?

may be you can increment the tag of the pop up (sorry, I am working on my iPad, I didn't check if it is possible for pop up menu), to keep track that a new selection occurred ; tag would be set to 0 in IB. You can even count the number of times it was changed, to help user if you see it does it a lot of times.

Thanks for the quick response, Claude. I can't find a Tag/tag item in any of the Inspectors for the PopUpButton. I have to believe, I could be wrong, that sets the "state" of the PUB to "unused/unselected" after ViewDidLoad and changes this state to "used/selected" when a selection is made. As I've said earlier, Microsoft chose to set the index to -1 to show this.

you can probably set the tag of NSMenuItemCell, so of the selected item ; may that enables to implement the solution ?


may be there is another property of NsPopupButton that you can use as a container.


Edit

there is an even simpler solution, unless I miss something:

- on windowDidLoad, you disable the validation button (or you set the state to disable in IB)

- when user selects the pop up, enable it if the selection has changed

- you can also increment a private property counter in the class to keep track on how many times the selection has changed (but may be detecting this IS your problem ?), if useful.

- You may have to provide user with a way to quit the view (cancel button for instance) when validation is disabled

Accepted Answer

viewDidLoad() could store the initially selected item of the pop-up in a property of the view controller, then you could later test to see if there has been a change of selection.


class ViewController: NSViewController {

    @IBOutlet weak var popUp: NSPopUpButton!
  
    private(set) var popUpInitiallySelectedItem: NSMenuItem?
  
    override func viewDidLoad() {
        super.viewDidLoad()
        popUpInitiallySelectedItem = popUp.selectedItem
    }

    @IBAction func popUpSelectionDidChange(_ sender: NSPopUpButton) {
        if sender.selectedItem === popUpInitiallySelectedItem {
            print("User has re-selected the initially selected item.")
        } else {
            print("User has selected some item other than the initially selected item.")
        }
    }
}

Thanks! That was a "big" help and solution that fills my need.

How to Determine if NSPopUpButton Has Changed?
 
 
Q