【发布时间】:2020-11-08 12:24:04
【问题描述】:
如何更改 UIAction 的状态?目标是在 UIMenu 内的 UIAction 旁边切换状态复选标记。
通过存储在视图控制器中的引用更改 UIAction 的 state 似乎根本不会更改状态。我错过了什么吗?
// View Controller
internal var menuAction: UIAction!
private func generatePullDownMenu() -> UIMenu {
menuAction = UIAction(
title: "Foo",
image: UIImage(systemName: "chevron.down"),
identifier: UIAction.Identifier("come.sample.action"),
state: .on
) { _ in self.menuAction.state = .off } // <--- THIS LINE
let menu = UIMenu(
title: "Sample Menu",
image: nil,
identifier: UIMenu.Identifier("com.sample.menu"),
options: [],
children: [menuAction]
)
return menu
}
// Inside UI setup code block
let buttonItem = UIBarButtonItem(
title: "",
image: UIImage(systemName: "chevron.down"),
primaryAction: nil,
menu: generatePullDownMenu()
)
尝试直接从闭包中更改action 状态并得到“操作是不可变的,因为它是菜单的子项”错误。现在我怀疑动作对象始终是不可变对象。
menuAction = UIAction(
title: "Foo",
image: UIImage(systemName: "chevron.down"),
identifier: UIAction.Identifier("come.sample.action"),
state: .on
) { action in action.state = .off } // <--- THIS LINE
【问题讨论】: