我在自定义 UIView 子类上使用 UIMenuController。这意味着它可以成为第一响应者,并在“删除”操作上声明它canPerformAction
。
我还希望此 View 的 super View (也是自定义 UIView)能够使用菜单 Controller ,因此在该 super View 上,我将其标记为能够成为第一响应者,并实现了 canPerformAction
用于不同的操作(在本例中为“复制”和“剪切”)。
事情是这样的——当我使菜单从(第一个) subview 可见时,它将所有三个操作都放在菜单中:删除、复制和剪切。在调试器中,我看到 canBecomeFirstResponder
和 canPerformAction
在菜单出现之前在两个 View 上被调用。
这是怎么回事?为什么菜单 Controller 不限制为第一响应者的 View ?或者我没有正确诊断这个问题?
谢谢。
最佳答案
您使用什么代码?
在 canPerformAction:withSender: 的文档中,
This default implementation of this method returns YES if the responder class implements the requested action and calls the next responder if it does not. ... Note that if your class returns NO for a command, another responder further up the responder chain may still return YES, enabling the command.
这似乎是矛盾的,说默认实现会递归响应者链,但如果返回 NO,UIMenuController 也会递归响应者链。
最简单的忽悠可能是覆盖 -nextResponder
以返回 nil,但这可能会产生其他副作用(例如,默认情况下,具有“nil”目标的操作会沿着响应者链向上! )。
关于iphone - UIMenuController和响应者链: what's going on?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642181/