确定 UIMenuController 何时被解除?

发布于 2024-09-17 10:54:56 字数 101 浏览 2 评论 0原文

有没有办法确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烙印 2024-09-24 10:54:57

斯威夫特5

NotificationCenter.default.addObserver(
        self,
        selector: #selector(willHideMenu),
        name: UIMenuController.willHideMenuNotification,
        object: nil)

Swift 5

NotificationCenter.default.addObserver(
        self,
        selector: #selector(willHideMenu),
        name: UIMenuController.willHideMenuNotification,
        object: nil)
拥醉 2024-09-24 10:54:56

当状态发生变化时,UIMenuController 将通知发布到默认的 NSNotification 中心。您可以订阅它们,以便在系统隐藏菜单时收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHideEditMenu:) name:UIMenuControllerWillHideMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didHideEditMenu:) name:UIMenuControllerDidHideMenuNotification object:nil];

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:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHideEditMenu:) name:UIMenuControllerWillHideMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didHideEditMenu:) name:UIMenuControllerDidHideMenuNotification object:nil];
木有鱼丸 2024-09-24 10:54:56

根据 @Markus Müller 的建议,您可以复制以下模式:

- (BOOL)becomeFirstResponder
{
    // starts listening for UIMenuControllerDidHideMenuNotification & triggers resignFirstResponder if received
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignFirstResponder) name:UIMenuControllerDidHideMenuNotification object:nil];
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerDidHideMenuNotification object:nil];

    // your custom cleanup code here (e.g. deselection)

    return [super resignFirstResponder];
}

在我的例子中,我有数百个可选择的对象,所以我不希望它们全部观察此通知全部时间到了!该模式在获得firstResponder时开始观察,在菜单关闭时触发resignFirstResponder,并在同一时间结束观察。

就我而言(如OP),由于该项目不可编辑,因此我希望在菜单关闭时调用resignFirstResponder。这样,无论他们是否选择某个菜单选项,resignFirstResponder 都会被调用,因此清理代码将始终触发。

Building on @Markus Müller's suggestion, here's a pattern you can copy:

- (BOOL)becomeFirstResponder
{
    // starts listening for UIMenuControllerDidHideMenuNotification & triggers resignFirstResponder if received
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignFirstResponder) name:UIMenuControllerDidHideMenuNotification object:nil];
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerDidHideMenuNotification object:nil];

    // your custom cleanup code here (e.g. deselection)

    return [super resignFirstResponder];
}

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.

撩动你心 2024-09-24 10:54:56

Swift 3 和4

NotificationCenter.default.addObserver(
   self,
   selector: #selector(self.didHideEditMenu),
   name: NSNotification.Name.UIMenuControllerDidHideMenu,
   object: nil)

NotificationCenter.default.addObserver(
   self,
   selector: #selector(self.willHideEditMenu),
   name: NSNotification.Name.UIMenuControllerWillHideMenu,
   object: nil)

Swift 3 & 4

NotificationCenter.default.addObserver(
   self,
   selector: #selector(self.didHideEditMenu),
   name: NSNotification.Name.UIMenuControllerDidHideMenu,
   object: nil)

NotificationCenter.default.addObserver(
   self,
   selector: #selector(self.willHideEditMenu),
   name: NSNotification.Name.UIMenuControllerWillHideMenu,
   object: nil)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文