UIMenu在CollectionView中使用

时间:2013-09-12 21:01:45

标签: ios uimenucontroller

这就是我设置popovers的方式

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Delete Patient"

                                                  action:@selector(customAction:)];



[[UIMenuController sharedMenuController] setMenuItems:@[menuItem]];

然后添加require方法

- (BOOL)canBecomeFirstResponder {

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;

}



- (void) customAction:(id) sender

{

for (Treatment *t in self.ptToDelete.patientRx) {

    [self.managedObjectContext deleteObject:t];

}



[self.managedObjectContext deleteObject:self.ptToDelete];



NSError *error = nil;

if (![self.managedObjectContext save:&error]) {

    NSLog(@"Error! %@", error);

}

}

适用于iOS6,但现在不适用。以下方法没有被调用,当我点击并按住

时应该调用它
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

1 个答案:

答案 0 :(得分:7)

我发现我需要在CollectionViewCell类中使用以下内容。但是,ios6中不需要这样做。希望这可以节省几个小时。

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

    // The selector/s should match your UIMenuItem selector
    if (action == @selector(customAction:)) {
        return YES;
    }
        return NO;
    }

- (void) customAction:(id)sender
{
    // do stuff
}