如何在运行时更改 UIMenu 内 UIAction 的状态?

Lor*_*lor 19 uikit ios swift

如何更改 UIAction 的状态?目标是切换 UIMenu 内 UIAction 旁边的状态复选标记。

在此输入图像描述

通过存储在视图控制器中的引用更改 UIActionstate似乎根本不会改变状态。我错过了什么吗?

// 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()
)
Run Code Online (Sandbox Code Playgroud)

尝试直接从闭包更改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
Run Code Online (Sandbox Code Playgroud)

Lor*_*lor 17

在状态更改时替换整个UIMenu对象就可以了。

// view controller
internal var barButton: UIBarButtonItem!

// UI setup function
barButton = UIBarButtonItem(
    image: UIImage(systemName: "arrow.up.arrow.down.square"),
    primaryAction: nil,
    menu: generatePullDownMenu()
)

// On state change inside UIAction 
let actionNextSeen = UIAction(
    title: "foo",
    image: UIImage(systemName: "hourglass", )
    state: someVariable ? .off : .on
) { _ in
    someVariable = false
    self.barButton.menu = self.generatePullDownMenu()
}
Run Code Online (Sandbox Code Playgroud)

参考

https://developer.apple.com/forums/thread/653862


Jos*_*osé 6

您将需要重新创建菜单。此示例还将正确选择被点击的项目:

private func setupViews()
    timeFrameButton = UIBarButtonItem(
        image: UIImage(systemName: "calendar"),
        menu: createMenu()
    )
    navigationItem.leftBarButtonItem = timeFrameButton
}

private func createMenu(actionTitle: String? = nil) -> UIMenu {
    let menu = UIMenu(title: "Menu", children: [
        UIAction(title: "Yesterday") { [unowned self] action in
            self.timeFrameButton.menu = createMenu(actionTitle: action.title)
        },
        UIAction(title: "Last week") { [unowned self] action in
            self.timeFrameButton.menu = createMenu(actionTitle: action.title)
        },
        UIAction(title: "Last month") { [unowned self] action in
            self.timeFrameButton.menu = createMenu(actionTitle: action.title)
        }
    ])
    
    if let actionTitle = actionTitle {
        menu.children.forEach { action in
            guard let action = action as? UIAction else {
                return
            }
            if action.title == actionTitle {
                action.state = .on
            }
        }
    } else {
        let action = menu.children.first as? UIAction
        action?.state = .on
    }
    
    return menu
}
Run Code Online (Sandbox Code Playgroud)


Ely*_*Ely 5

使用 iOS 15 或更高版本时,您可以使用UIDeferredMenuElement.uncached创建动态UIAction对象:

let menu = UIMenu(options: .displayInline, children: [
    UIDeferredMenuElement.uncached { [weak self] completion in
        let actions = [
            UIAction(title: "My Dynamic Action 1", image: UIImage(systemName: "hourglass"), state: self?.myState1 == true ? .on : .off, handler: { (action) in
                // Do something
            }),
            UIAction(title: "My Dynamic Action 2", image: UIImage(systemName: "star"), state: self?.myState2 == true ? .on : .off, handler: { (action) in
                // Do something else
            })
        ] 
        completion(actions)
    }
])
Run Code Online (Sandbox Code Playgroud)

文档:https ://developer.apple.com/documentation/uikit/uideferredmenuelement/3857602-elementwithuncachedprovider