UIMenuController 和响应者链:发生了什么?
我在自定义 UIView 子类上使用 UIMenuController。这意味着它可以成为第一响应者,并在“删除”操作上声明它canPerformAction
。
我还希望此视图的超级视图(也是自定义 UIView)能够使用菜单控制器,因此在该超级视图上,我已将其标记为能够成为第一响应者,并实现了 canPerformAction
不同的操作(在本例中为“复制”和“剪切”)。
事情是这样的——当我使菜单从(第一个)子视图可见时,它将所有三个操作都放在菜单中:删除、复制和剪切。在调试器中,我看到在菜单出现之前,两个视图上都调用了 canBecomeFirstResponder
和 canPerformAction
。
这是怎么回事?为什么菜单控制器不限制为第一响应者的视图?还是我诊断不正确?
谢谢。
I am using the UIMenuController on a custom UIView subclass. This means it can become first responder, and claims it canPerformAction
on the "delete" action.
I would also like this view's superview (also a custom UIView) to be able to use the menu controller, so on that superview, I have marked it as being able to be first responder, and implemented canPerformAction
for different actions ("copy" and "cut" in this case).
Here's the thing-- when I make the menu visible from the (first) subview, it puts all three actions in the menu: delete, copy, and cut. In the debugger, I see canBecomeFirstResponder
and canPerformAction
being invoked on both views before the menu appears.
What's going on here? Why isn't the menu controller restricted to the view that's become first responder? Or am I not diagnosing this correctly?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用什么代码?
在 canPerformAction:withSender:,
这似乎是矛盾的,说默认实现会递归响应者链,但如果返回 NO,UIMenuController 也会递归响应者链。
最简单的忽悠可能是覆盖
-nextResponder
以返回 nil,但这可能会产生其他副作用(例如,默认情况下,具有“nil”目标的操作会沿着响应者链向上!)。What code are you using?
In the documentation for canPerformAction:withSender:,
It seems to be contradictory, saying the default implementation recurses up the responder chain, but then also that UIMenuController recurses up the responder chain if you return NO.
The easiest fudge is probably to override
-nextResponder
to return nil, but that might have other side-effects (for one, actions with a "nil" target go up the responder chain by default!).