I have a uibutton with a menu and several actions.
Whenever the user select a menu item I want to fetch it and print it.
This is my button:
private lazy var measurementField: UIButton = {
let saveAction = { (action: UIAction) in
//action
}
let saveMenu = UIMenu(children: [
UIAction(title: Ingredient.Measurements.oz.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.grams.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.cups.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.tsp.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.tbsp.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.quarts.rawValue, image: nil, handler: saveAction),
UIAction(title: Ingredient.Measurements.pints.rawValue, image: nil, handler: saveAction),
])
var config = UIButton.Configuration.filled()
config.baseBackgroundColor = .label
let button = UIButton(configuration: config)
button.menu = saveMenu
button.showsMenuAsPrimaryAction = true
button.changesSelectionAsPrimaryAction = true
button.addTarget(CreateFormViewController(), action: #selector(CreateFormViewController.linkData), for: .touchUpInside)
return button
}()
This is my selector (the action that should be triggered):
@objc public func linkData(..more params... , _ sender:UIButton?) {
...
print(sender?.menu?.title)
...
}
It only prints 'nil' tho. How can I fetch that menu-selected item?