2

This is how i set up the popovers

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

                                                  action:@selector(customAction:)];



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

and then add the require methods

- (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);

}

}

This works for iOS6, but now it is not. The following method is not getting called, it should be called when I tap and hold

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
DogCoffee
  • 19,820
  • 10
  • 87
  • 120

1 Answers1

7

I found that I needed to have the following in my CollectionViewCell class. This was not required in ios6 however. Hope this saves someone a few hrs.

- (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
}
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • I found the same thing. Same issue here: http://stackoverflow.com/questions/18906991/uimenucontroller-sharedmenucontroller-custom-menuitem-for-uicollectionview-do#comment27991500_18906991 – nicolas Sep 23 '13 at 07:51
  • for me somehow it did work like the answer i wrote in this post: http://stackoverflow.com/a/18725322/1944351 No need for adding the method above when you follow my answer exactly. Note: I did not add canBecomeFirstResponder too (it is not needed) – Nilz11 Sep 28 '13 at 12:03
  • for me it was an issue with UISearchBar under iOS7. See http://stackoverflow.com/a/19183509/388412 – auco Oct 04 '13 at 14:15