ios - buildMenu 在 AppDelegate 中被调用,而不是 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")
}
但是,我需要根据用户在应用程序中的位置更改菜单中可用的选项,因此我尝试在 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()
}
然后再次..
// This is never called
override func buildMenu(with builder: UIMenuBuilder) {
print("Updating menu in View Controller")
}
但是 UIViewController 中的 buildMenu 永远不会被调用:(
如果这是预期的行为或是否有任何解决方法,有什么想法吗?
解决方案
对于主菜单,系统只参考UIApplication
和UIApplicationDelegate
,因为主菜单可以在没有任何窗口的情况下存在,因此没有任何UIViewController
层次结构。UIViewController
这就是为什么主菜单不会调用您的覆盖。
对于上下文菜单,系统确实会从视图开始咨询完整的响应者链。
如果您需要根据上下文更新主菜单命令:
- 您可以离开
buildMenu(with:)
,UIApplicationDelegate
安排代表弄清楚何时以及发生了什么变化,并在发生变化UIMenuSystem.main.setNeedsRebuild()
时致电,或者 buildMyMenu(with:)
您可以在子类中定义一个私有方法UIViewController
,并安排buildMenu(with:)
inUIApplicationDelegate
调用它,或者- 您可以在 中构建一个静态菜单
buildMenu
,并依靠您的覆盖canPerformAction(_:withSender:)
和validate(_:)
启用或禁用甚至隐藏特定命令,例如通过更新覆盖attributes
中的属性validate(_:)
。
推荐阅读
- python - 仅基于条件、groupby 和某些行,用以前的行值填充行
- python - 如何以长期顺序使用多处理?
- javascript - 无法使用three.js显示3D obj文件+纹理
- javascript - TypeError:.then 不是函数
- c++ - 使用柯南将 PahoMqttCpp 链接为 CMake 中的静态库
- javascript - Javascript将数据库日期时间更改为HTML输入日期时间
- javascript - 从 JSON 中获取字符串(base64 编码的图像)
- python - 使用 pandas 选择一系列特定行
- typescript - NestJS DTO 类设置 class-validator 和 class-transformer 执行顺序
- python - 如何通过迭代定义的函数将值填充到新列中?