iOS:具有单个选择器的多个 UIMenuItem

Posted

技术标签:

【中文标题】iOS:具有单个选择器的多个 UIMenuItem【英文标题】:iOS: Multiple UIMenuItems with single selector 【发布时间】:2013-06-11 05:27:05 【问题描述】:

我是 ios 开发新手,我正在开发 UIMenuController。看起来我们需要为每个 UIMenuItem 设置不同的选择器。

有没有办法让一个选择器确定我点击了哪个项目?

我们可以向选择器发送一个参数,以便我们可以识别我们点击了哪个项目吗?

这是我初始化菜单项的方式。

UIMenuItem *item = [[UIMenuItem alloc]initWithTitle:@"Item 1" action:@selector(itemClicked:)];

【问题讨论】:

虽然不在文档中,但我怀疑 @selector(itemClicked:) 会将其参数设置为单击的 UIMenuItem 实例。如果是这种情况,您可以尝试并报告吗? 我试过这个,但参数设置为 UIMenuController。我不认为我可以用它来识别点击的项目。 不幸的是。那你真的别无选择。 如果没有其他办法,那就太糟糕了...如果我需要一些 30 个菜单项...定义 30 个选择器怎么办?我可能不得不寻找替代方案... ***.com/questions/9146670/… 【参考方案1】:

您可以像这样使用块来处理委托

UIMenuItem.h

@property (nonatomic, copy) void (^onButtonClicked)(id btn);

UIMenuItem.m

@synthesize onButtonClicked;

- (IBAction)btnExpandClicked:(id)sender

  self.onButtonClicked(sender);

将连接到每个菜单项

然后在你的 UITableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath 
...
item.onButtonClicked = ^(id btn) 
        // code that will run when the menu item is clicked "not the row of the table"
        // btn is the menu button clicked, you can implement a pop up based on each menu button clicked ( based on the tag for example )
    ;

【讨论】:

以上是关于iOS:具有单个选择器的多个 UIMenuItem的主要内容,如果未能解决你的问题,请参考以下文章

UIMenuContoller UIMenuItem 具有多个参数的自定义操作?

IOS UIMenuController UIMenuItem,如何确定使用通用选择器方法选择的项目

Promise.allSettled 中多个选择器的 TestCafe 断言似乎具有误导性

IOS5 中的自定义 UIMenuItem 太宽

具有单个 uiviewcontroller 和多个视图的自动布局 iOS

逗号分隔列表的特殊性规则