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.
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.")
}
}
}