I have this particular code
let genderOptions = ["Male".localized, "Female".localized]
.map { UIAction(title: $0, state: .off, handler: {_ in }) }
genderButton.changesSelectionAsPrimaryAction = true
genderButton.menu = UIMenu(children: genderOptions)
I have set all the UIAction
's state
to .off
, but still it is selecting the first item by default. Below is the Apple's documentation.
But when I print the selectedElements
, it prints the following:
print(genderButton.menu?.selectedElements)
even though all the
UIAction
items state = .off
, it prints an array with the first item as selected.
Q: How to have an initial state without any selection? Is popup button a good option to use here or should I use UITextField with UIPickerView? If anyone could give a complete solution, it would be great.
Assuming you have set
changesSelectionAsPrimaryAction = true
, you can add a hidden (with the.hidden
attribute) action as the first action:The button selects the first action and uses its title as the button's title. The user can then select one of the other two actions. But they cannot select the hidden action.
The
.disabled
attribute also prevents the user from selecting the action, but the option is still visible.See also:
UIMenuElement.Attributes