objective-c - 带有自定义项的 UIMenuController 不适用于 UICollectionview

标签 objective-c uicollectionview uimenucontroller uiresponder

我在长按 UICollectionViewCell 时添加了自定义菜单 Controller

    [self becomeFirstResponder];
    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Custom Action"
                                                      action:@selector(customAction:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
    [[UIMenuController sharedMenuController] setTargetRect: self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];

canBecomeFirstResponder 也被调用了

- (BOOL)canBecomeFirstResponder {
    // NOTE: This menu item will not show if this is not YES!
    return YES;
}

//这个方法没有被调用

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    NSLog(@"canPerformAction");
    // The selector(s) should match your UIMenuItem selector
    if (action == @selector(customAction:)) {
        return YES;
    }
    return NO;
}

我也实现了这些方法

- (BOOL)collectionView:(UICollectionView *)collectionView
      canPerformAction:(SEL)action
    forItemAtIndexPath:(NSIndexPath *)indexPath
            withSender:(id)sender {


    if([NSStringFromSelector(action) isEqualToString:@"customAction:"]){
        NSLog(@"indexpath : %@",indexPath);
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"warning.." message:@"Do you really want to delete this photo?" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alertview show];
        return YES;
    }

    return YES;

}

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

- (void)collectionView:(UICollectionView *)collectionView
         performAction:(SEL)action
    forItemAtIndexPath:(NSIndexPath *)indexPath
            withSender:(id)sender {
    NSLog(@"performAction");
}

虽然它只显示“剪切、复制和粘贴”菜单

最佳答案

也许有点晚了,但我可能为那些仍在寻找这个的人找到了更好的解决方案:

在您的 UICollectionViewController 的 viewDidLoad 中添加您的项目:

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Title" action:@selector(action:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];

添加以下委托(delegate)方法:

//This method is called instead of canPerformAction for each action (copy, cut and paste too)
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
        if (action == @selector(action:)) {
            return YES;
        }
        return NO;
    }
    //Yes for showing menu in general
    - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }

子类 UICollectionViewCell 如果你还没有的话。添加您为项目指定的方法:

- (void)action:(UIMenuController*)menuController {

}

这样您就不需要任何 becomeFirstResponder 或其他方法。您可以在一个地方执行所有操作,并且如果您调用将单元格本身作为参数的通用方法,则可以轻松处理不同的单元格。

编辑:uicollectionview 不知何故需要此方法的存在(您的自定义操作不会调用此方法,我认为 uicollectionview 只是检查是否存在)

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

}

关于objective-c - 带有自定义项的 UIMenuController 不适用于 UICollectionview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17024642/

相关文章:

iphone - UIViews-masked-off-area-still-touchable

ios - UICollectionView,只是让单元格适合宽度?

ios - 自定义 UIMenuController 问题

iphone - 如何从 UIMenuController 中删除复制、全选、定义菜单项

iphone - 在 iphone 的 UIWebView 中显示选择的自定义菜单

ios - 在 iOS 中将两个文本字段添加/连接到一个标签中

objective-c - iOS 应用程序中 _class_initialize 中的 semaphore_wait_signal_trap 死锁

objective-c - 更喜欢 if 评价

ios - UICollectionView 使用组合布局在同一部分中查看多个单元格类型

ios - 当我在 numberOfItemsInSection 中返回 1 时,cellForItemAt 未在 Swift 中调用