带有 @selector 和动态方法的动态 UIMenuItems

发布于 2024-09-10 01:43:06 字数 3172 浏览 5 评论 0原文

我正在尝试使用 UIMenuController 作为动态菜单(标题和操作来自服务器)。问题是我必须使用 UIMenuItems initWithTitle:action: 其中 action 是 @selector。

我可以使用 @selector(dispatch:) 但随后我无法区分用户按下了哪些项目。 - (void)dispatch:(id)sender { NSLog(@"%@", sender); } 说它是一个 UIMenuController 并且它没有可以告诉按下哪个菜单项的方法。

我不能只编写 100 个方法来分派每个可能的选择器,好吧,不会超过 10 个,但这似乎不是一个好主意。

我是否必须为每个这样的选择器创建动态方法? http://developer.apple.com/mac /library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html?这似乎也很奇怪。

还有比这两个更好的建议吗?

// 这种方法行不通。

- (void)showMenu {

    [self becomeFirstResponder];

    NSMutableArray *menuItems = [[NSMutableArray alloc] init];

    UIMenuItem *item;
    for (MLAction *action in self.dataSource.actions) {
        item = [[UIMenuItem alloc] initWithTitle:action.title action:@selector(action:)];
        [menuItems addObject:item];
        [item release];
    }

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = menuItems;
    [menuItems release];
    [menuController update];
    [menuController setMenuVisible:YES animated:YES];

}

- (void)action:(id)sender {
    NSLog(@"%@", sender); // gives UIMenuController instead of UIMenuItem
    // I can not know which menu item was pressed
}

// 这种方法实在是丑陋。

- (void)showMenu {

    [self becomeFirstResponder];

    NSMutableArray *menuItems = [[NSMutableArray alloc] initWithCapacity:5];

    UIMenuItem *item;
    NSInteger i = 0;
    for (MLAction *action in self.dataSource.actions) {
        item = [[UIMenuItem alloc] initWithTitle:action.text
                                                                            action:NSSelectorFromString([NSString stringWithFormat:@"action%i:", i++])];
        [menuItems addObject:item];
        [item release];
    }

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = menuItems;
    [menuItems release];
    [menuController update];
    [menuController setMenuVisible:YES animated:YES];

}

- (void)action:(NSInteger)number {
    NSLog(@"%i", number); // gives the index of the action in the menu.
}

// This is a hack, I have to assume that there will never be more then 15 actions
- (void)action0:(id)sender { [self action:0]; }
- (void)action1:(id)sender { [self action:1]; }
- (void)action2:(id)sender { [self action:2]; }
- (void)action3:(id)sender { [self action:3]; }
- (void)action4:(id)sender { [self action:4]; }
- (void)action5:(id)sender { [self action:5]; }
- (void)action6:(id)sender { [self action:6]; }
- (void)action7:(id)sender { [self action:7]; }
- (void)action8:(id)sender { [self action:8]; }
- (void)action9:(id)sender { [self action:8]; }
- (void)action10:(id)sender { [self action:10]; }
- (void)action11:(id)sender { [self action:11]; }
- (void)action12:(id)sender { [self action:12]; }
- (void)action13:(id)sender { [self action:13]; }
- (void)action14:(id)sender { [self action:14]; }

I am trying to use UIMenuController for a dynamical menu (titles and actions come from a server). The problem is that I have to use UIMenuItems initWithTitle:action: where action is a @selector.

I can use @selector(dispatch:) but then I am not able to distinguish which of the items the user pressed. - (void)dispatch:(id)sender { NSLog(@"%@", sender); } says it is a UIMenuController and It don't have a method which would tell which menu item was pressed.

I can't just write 100 methods to dispatch every possible selector, ok there will not be more then 10 but still, this seems not a good idea.

Do I have to create dynamic methods for each such selector? http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html? This seems odd too.

Any better propositions then this two?

// This approach doesn't work.

- (void)showMenu {

    [self becomeFirstResponder];

    NSMutableArray *menuItems = [[NSMutableArray alloc] init];

    UIMenuItem *item;
    for (MLAction *action in self.dataSource.actions) {
        item = [[UIMenuItem alloc] initWithTitle:action.title action:@selector(action:)];
        [menuItems addObject:item];
        [item release];
    }

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = menuItems;
    [menuItems release];
    [menuController update];
    [menuController setMenuVisible:YES animated:YES];

}

- (void)action:(id)sender {
    NSLog(@"%@", sender); // gives UIMenuController instead of UIMenuItem
    // I can not know which menu item was pressed
}

// This approach is really ugly.

- (void)showMenu {

    [self becomeFirstResponder];

    NSMutableArray *menuItems = [[NSMutableArray alloc] initWithCapacity:5];

    UIMenuItem *item;
    NSInteger i = 0;
    for (MLAction *action in self.dataSource.actions) {
        item = [[UIMenuItem alloc] initWithTitle:action.text
                                                                            action:NSSelectorFromString([NSString stringWithFormat:@"action%i:", i++])];
        [menuItems addObject:item];
        [item release];
    }

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = menuItems;
    [menuItems release];
    [menuController update];
    [menuController setMenuVisible:YES animated:YES];

}

- (void)action:(NSInteger)number {
    NSLog(@"%i", number); // gives the index of the action in the menu.
}

// This is a hack, I have to assume that there will never be more then 15 actions
- (void)action0:(id)sender { [self action:0]; }
- (void)action1:(id)sender { [self action:1]; }
- (void)action2:(id)sender { [self action:2]; }
- (void)action3:(id)sender { [self action:3]; }
- (void)action4:(id)sender { [self action:4]; }
- (void)action5:(id)sender { [self action:5]; }
- (void)action6:(id)sender { [self action:6]; }
- (void)action7:(id)sender { [self action:7]; }
- (void)action8:(id)sender { [self action:8]; }
- (void)action9:(id)sender { [self action:8]; }
- (void)action10:(id)sender { [self action:10]; }
- (void)action11:(id)sender { [self action:11]; }
- (void)action12:(id)sender { [self action:12]; }
- (void)action13:(id)sender { [self action:13]; }
- (void)action14:(id)sender { [self action:14]; }

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

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

发布评论

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

评论(2

掀纱窥君容 2024-09-17 01:43:06

这种方法是可行的,尽管您需要为每个按钮提供一个唯一的选择器名称,以及从该名称到您想要的目标的映射。
对于选择器名称,必须选择唯一的字符串(UUID 或标题的经过清理和前缀的版本也可以)。然后,您需要一种方法来解析调用,并使用不同的选择器名称为其设置“别名”:

- (void)updateMenu:(NSArray *)menuEntries {
    Class cls = [self class];
    SEL fwd = @selector(forwarder:);
    for (MenuEntry *entry in menuEntries) {
        SEL sel = [self uniqueActionSelector];
        // assuming keys not being retained, otherwise use NSValue:
        [self.actionDict addObject:entry.url forKey:sel]; 
        class_addMethod(cls, sel, [cls instanceMethodForSelector:fwd], "v@:@");
        // now add menu item with sel as the action
    }
}

现在转发器可以查找与菜单项关联的 URL:

- (void)forwarder:(UIMenuController *)mc {
    NSLog(@"URL for item is: %@", [actionDict objectForKey:_cmd]);
}

要生成选择器,您可以使用某些东西喜欢:

- (SEL)uniqueActionSelector {
    NSString *unique = ...; // the unique part
    NSString *selString = [NSString stringWithFormat:@"menu_%@:", unique];
    SEL sel = sel_registerName([selString UTF8String]);
    return sel;
}

That approach would work, although you need a unique selector-name for every button and a mapping from that name to whatever you want to target.
For the selector name a unique string has to be chosen (UUIDs or maybe a sanitized & prefixed version of the title would work). Then you need one method that resolves the call and "alias" it with the different selector names:

- (void)updateMenu:(NSArray *)menuEntries {
    Class cls = [self class];
    SEL fwd = @selector(forwarder:);
    for (MenuEntry *entry in menuEntries) {
        SEL sel = [self uniqueActionSelector];
        // assuming keys not being retained, otherwise use NSValue:
        [self.actionDict addObject:entry.url forKey:sel]; 
        class_addMethod(cls, sel, [cls instanceMethodForSelector:fwd], "v@:@");
        // now add menu item with sel as the action
    }
}

Now the forwarder can look up what URL is associated with the menu item:

- (void)forwarder:(UIMenuController *)mc {
    NSLog(@"URL for item is: %@", [actionDict objectForKey:_cmd]);
}

To generate the selectors you could use something like:

- (SEL)uniqueActionSelector {
    NSString *unique = ...; // the unique part
    NSString *selString = [NSString stringWithFormat:@"menu_%@:", unique];
    SEL sel = sel_registerName([selString UTF8String]);
    return sel;
}
烟沫凡尘 2024-09-17 01:43:06

除非菜单项执行相同的操作,否则为什么它们应该共享一个操作?我将继续编写指定您想要的行为的操作,并将菜单项链接到这些操作。

Unless the menu items do the same thing, why should they share an action? I would go ahead and write actions that specify a behavior you want and link the menu items up to those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文