无法让UIMenuController显示自定义项目

And*_*ebe 4 objective-c uimenucontroller uicollectionview 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];
Run Code Online (Sandbox Code Playgroud)

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

我没有完全实现这一点,还是我没有做到这一点?

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

小智 6

我可以按照此链接(/sf/answers/953274871/)上的说明进行即兴创作,从而在UICollectionViewCell上实现自定义菜单.

在我的UICollectionViewController中,我通过将它们添加到菜单控制器中来实现自定义菜单项,如链接中所示.

然后我在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));
}
Run Code Online (Sandbox Code Playgroud)

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

这些操作必须与Collection Controller中实现的操作相同.

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

这可能不是最好的方式,但对我有用.


mat*_*att 4

你是对的。无法自定义长按表视图单元格或集合视图单元格时显示的菜单。

我在书中讨论了这个问题:

http://www.apeth.com/iOSBook/ch21.html#_table_view_menus

正如我所说,复制、剪切和粘贴是您唯一的选择。如果您想自定义菜单,则必须使菜单源自其他内容。

编辑:在我的书的 iOS 7 版本中,我演示了一种方法来做到这一点。表视图单元格和集合视图单元格是相同的,因此我将从表视图单元格解决方案开始。诀窍是您必须在单元子类中实现操作方法。例如,如果您的自定义操作选择器是abbrev:,则必须对该单元进行子类化并实现abbrev:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch08p454tableCellMenus2/ch21p718sections/MyCell.m

这是唯一棘手的部分。然后,回到控制器类,您可以按照abbrev:对任何菜单所做的操作进行操作。在 中shouldShowMenuForRowAtIndexPath:,将其添加到自定义菜单中。然后实现canPerformAction:performAction:正如您所期望的那样(一直滚动到底部):

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch08p454tableCellMenus2/ch21p718sections/RootViewController.m

这是集合视图单元格的并行实现:单元格子类:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/Cell.m

和控制器(一直滚动到底部):

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/ViewController.m

在我的书的 iOS 8 版本中,这些方法也被翻译成 Swift(并非没有一些困难)。