小编Dav*_*ave的帖子

在AppDelegate中调用buildMenu,但不调用UIViewController

我正在尝试为我的应用程序中的每个视图创建一个自定义菜单,但是看来buildMenu没有在视图控制器中调用。这是一个例子:

在我的AppDelegate中,使用了此代码,该代码可以按预期100%运行。

override func buildMenu(with builder: UIMenuBuilder) {

    print("Updating menu from AppDelegate")

    super.buildMenu(with: builder)

    let command = UIKeyCommand(
        input: "W",
        modifierFlags: [.command],
        action: #selector(self.helloWorld(_:))
    )
    command.title = "Hello"

    builder.insertChild(UIMenu(
        __title: "World",
        image: nil,
        identifier: UIMenu.Identifier(rawValue: "com.hw.hello"),
        options: [],
        children: [command]
    ), atEndOfMenu: .file)
}

@objc private func helloWorld(_ sender: AppDelegate) {

    print("Hello world")
}
Run Code Online (Sandbox Code Playgroud)

但是我需要根据用户在应用程序中的位置来更改菜单中可用的选项,因此我尝试在UIViewController中执行此操作:

override func viewDidAppear(_ animated:Bool){
  // Tried all of these to see if any work
    UIMenuSystem.main.setNeedsRebuild()
    UIMenuSystem.context.setNeedsRebuild()
    UIMenuSystem.main.setNeedsRevalidate()
    UIMenuSystem.context.setNeedsRevalidate() 
}
Run Code Online (Sandbox Code Playgroud)

然后再次..

// This is never called …
Run Code Online (Sandbox Code Playgroud)

ios ios13 mac-catalyst uimenu uimenubuilder

5
推荐指数
1
解决办法
358
查看次数

标签 统计

ios ×1

ios13 ×1

mac-catalyst ×1

uimenu ×1

uimenubuilder ×1