iOS:如何从 UIMenuController 获取选定的 UIMenuItem

发布于 2024-12-25 09:19:38 字数 349 浏览 1 评论 0原文

我正在尝试使用 UIMenuCnotroller 来显示动态生成的项目列表,它们共享相同的操作方法,因此我需要知道在单个操作方法中选择了哪个项目。

然而,在action方法- (void)menuItemAction:(id)sender;发送者实际上是UIMenuController对象,我没有找到的任何方法>UIMenuController 可以告诉我选择了哪个菜单项。

我能想到的一种解决方案是为不同的项目动态生成不同的操作选择器,并在 forwardInspiration 中做一些技巧,

但是有没有更简单的方法呢?

I am trying to use UIMenuCnotroller to show a list of dynamically generated items, they share the same action method, and so I need to know which item is selected in the single action method.

However, in the action method - (void)menuItemAction:(id)sender;the sender is actually the UIMenuController object, and I didn't find any method of UIMenuController can tell me which menuitem is selected.

One solution I can think of is to dynamically generate different action selectors for different items, and do some tricks in forwardInvocation

But is there any easier way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

故事未完 2025-01-01 09:19:38

您可以使用 UIMenuCnotroller ,例如:
1)创建:

UIMenuController *menuController = [UIMenuController sharedMenuController];
        UIMenuItem *open = [[UIMenuItem alloc] initWithTitle:@"Open" action:@selector(open:)];
        UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:@"Re-Download" action:@selector(reDownload:)];

        [menuController setMenuItems:[NSArray arrayWithObjects:open, reDownload, nil]];
        [menuController setTargetRect:cell.frame inView:self.view];
        [menuController setMenuVisible:YES animated:YES];

        [open release];
        [reDownload release];

2)要捕获动作应该实现下一个方法:

- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender 
{
    if (selector == @selector(open:))
    {
        return YES;
    }

    if (selector == @selector(reDownload:))
    {
        return YES;
    }

    return NO;
}

- (BOOL) canBecomeFirstResponder 
{
    return YES;
}

3)并实现你的方法:

- (void) open:(id) sender 
{
    [self doSomething];
}

- (void) reDownload:(id) sender 
{
[self doSomething];
}

希望,这会有所帮助。

You can use UIMenuCnotroller like:
1) creation:

UIMenuController *menuController = [UIMenuController sharedMenuController];
        UIMenuItem *open = [[UIMenuItem alloc] initWithTitle:@"Open" action:@selector(open:)];
        UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:@"Re-Download" action:@selector(reDownload:)];

        [menuController setMenuItems:[NSArray arrayWithObjects:open, reDownload, nil]];
        [menuController setTargetRect:cell.frame inView:self.view];
        [menuController setMenuVisible:YES animated:YES];

        [open release];
        [reDownload release];

2) To catch actions should implement next methods:

- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender 
{
    if (selector == @selector(open:))
    {
        return YES;
    }

    if (selector == @selector(reDownload:))
    {
        return YES;
    }

    return NO;
}

- (BOOL) canBecomeFirstResponder 
{
    return YES;
}

3) And realization of yours methods:

- (void) open:(id) sender 
{
    [self doSomething];
}

- (void) reDownload:(id) sender 
{
[self doSomething];
}

Hope, this helps.

蹲墙角沉默 2025-01-01 09:19:38

好的,我已经解决了这个问题。它涉及到搞乱 [NSObjectforwardInspiration:] 并且有点脏,但生成的代码非常少。在这里回答:https://stackoverflow.com/a/9874092/790036

Okay, I've solved this one. It involves messing with [NSObject forwardInvocation:] and is a bit dirty, but the resulting code is quite minimal. Answered over here: https://stackoverflow.com/a/9874092/790036

琉璃梦幻 2025-01-01 09:19:38

一种最简单的方法是对每个菜单项使用不同的@selector方法

示例:

UIMenuItem *oneObj = [[UIMenuItem alloc] initWithTitle:@"One" action:@selector(One:)];

UIMenuItem *twoObj = [[UIMenuItem alloc] initWithTitle:@"Two" action:@selector(Two:)];

One easiest way would be to use different @selector method for each menu item

Examples:

UIMenuItem *oneObj = [[UIMenuItem alloc] initWithTitle:@"One" action:@selector(One:)];

UIMenuItem *twoObj = [[UIMenuItem alloc] initWithTitle:@"Two" action:@selector(Two:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文