objective-c - 无法让 UIMenuController 显示自定义项目

标签 objective-c uicollectionview uimenucontroller uimenuitem

所以,我不确定我在这里是否做错了什么,但我有一个 UIViewController,上面有一个 UICollectionView。在 UIViewController 的 viewDidLoad 方法中,我执行以下操作,它不会向显示的弹出窗口添加任何自定义菜单项。

UIMenuItem *removeItem = [[UIMenuItem alloc] initWithTitle:@"Remove" action:@selector(handleRemoveItem:)];
UIMenuItem *duplicateItem = [[UIMenuItem alloc] initWithTitle:@"Duplicate" action:@selector(handleDuplicateItem:)];

[[UIMenuController sharedMenuController] setMenuItems:@[removeItem, duplicateItem]];

[removeItem release];
[duplicateItem release];

我确实设置了 collectionView:shouldShowMenuForItemAtIndexPath:collectionView:canPerformAction:forItemAtIndexPath:withSender: 在所有情况下都返回 YES,但是无论如何,只有剪切、复制和粘贴会出现。

我是不是没有完全实现,还是没有做对?

附言- 我确实在整个谷歌中查看了尽可能多的例子,但我没有找到任何有用的东西。

最佳答案

我能够按照此链接 (https://stackoverflow.com/a/13618212/2313416) 上的说明进行一些即兴创作,从而在 UICollectionViewCell 上实现自定义菜单。

在我的 UICollectionViewController 中,我通过将它们添加到链接中的菜单 Controller 来实现自定义菜单项。

然后我在 UICollectionViewController 中实现了以下内容:

- (BOOL)collectionView:(UICollectionView *)cv canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}

- (BOOL)collectionView:(UICollectionView *)cv shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

- (void)collectionView:(UICollectionView *)cv performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

NSLog(@"perform action:%@", NSStringFromSelector(action));
}

在我的 UICollectionViewCell 中,我实现了类似于以下内容:

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

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

if (action == @selector(onCustom2:)) {
    return YES;
} 
return NO;
}

这些操作必须与在集合 Controller 中实现的相同。

如果想要包含复制或粘贴功能,请将它们添加到 canPerformAction:,然后更改 collectionView::canPerformAction: 以返回 YES。

这可能不是最好的方法,但对我有用。

关于objective-c - 无法让 UIMenuController 显示自定义项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778017/

相关文章:

iphone - 如何以编程方式清除 iPhone 上的浏览​​器缓存?

ios - 在 swift ios 应用程序中添加手势识别器的最佳原则

ios - Collection View 高度

objective-c - UITEXTVIEW:获取最近在uitextview中输入的单词

ios - Sinch:如何从被叫方的锁定屏幕中关闭 Apple CallKit UI

ios - 更新应用程序时如何强制删除 NSUserDefaults?

objective-c - NSTextField 的单元格 setPlaceholderAttributedString - 获得焦点时占位符不可见

swift - UICollectionView 在一个部分中有两个自定义单元格

ios - 捕获复制/粘贴事件 UIMenuController

ios - 如何确定用户何时复制了文本