UIMenu popovers in CollectionView

♀尐吖头ヾ 提交于 2019-12-22 15:23:06

问题


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

回答1:


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
}


来源:https://stackoverflow.com/questions/18774265/uimenu-popovers-in-collectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!