我正在尝试为我的应用程序中的每个视图创建一个自定义菜单,但是看来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)