If we tried to create an NSPopupButton
in swift/objective-c we find that if we don't set pullsdown to false, once an item other than the 1st item is selected you would never be able to select the first item back.
class PopupButton: NSPopUpButton {
let items = ["item 1", "item 2", "item 3"]
override init(frame buttonFrame: NSRect, pullsDown flag: Bool) {
super.init(frame: buttonFrame, pullsDown: flag)
items.forEach { item in
menu?.addItem(withTitle: item, action: #selector(handleSelectedItem(_:)), keyEquivalent: "")
}
}
@objc private func handleSelectedItem(_ selectedItem: NSMenuItem) {
title = selectedItem.title
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Is there a way where I could fix this? (Note: I don't want answers that say to set pullsDown to false and issue is fixed. I need to keep the behaviour of the PopupButton as expected.)