确定 UIMenuController 何时被解除?
有没有办法确定 UIMenuController 何时被解雇?我有一个(不可编辑的)文本区域,当菜单出现时我会突出显示,并且当他们选择一个项目(简单)或取消(不可能?)时我想取消突出显示它
Is there a way to determine when a UIMenuController has been dismissed? I have a (non-editable) text area I'm highlighting when the menu is brought up, and I'd like to un-highlight it when they either select an item (easy) or cancel (not possible?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
斯威夫特5
Swift 5
当状态发生变化时,UIMenuController 将通知发布到默认的 NSNotification 中心。您可以订阅它们,以便在系统隐藏菜单时收到通知:
On state changes UIMenuController posts notifications to the default NSNotification center. You can subscribe to them to get notified when the system hides the menu:
根据 @Markus Müller 的建议,您可以复制以下模式:
在我的例子中,我有数百个可选择的对象,所以我不希望它们全部观察此通知全部时间到了!该模式在获得firstResponder时开始观察,在菜单关闭时触发
resignFirstResponder
,并在同一时间结束观察。就我而言(如OP),由于该项目不可编辑,因此我希望在菜单关闭时调用
resignFirstResponder
。这样,无论他们是否选择某个菜单选项,resignFirstResponder
都会被调用,因此清理代码将始终触发。Building on @Markus Müller's suggestion, here's a pattern you can copy:
In my case I have hundreds of selectable objects, so I didn't want them all observing this notification all the time! What this pattern does start observing when it gains firstResponder, triggers
resignFirstResponder
when the menu is dismissed, and ends observing in the same.In my my case (as in the OP's), as the item is uneditable it is desirable for me to call
resignFirstResponder
when the menu is dismissed. This way,resignFirstResponder
gets called if whether they select one of the menu options, or not, so the cleanup code will always fire.Swift 3 和4
Swift 3 & 4