Maybe a little late, but I may have found a better solution for those who are still looking for this:
In the viewDidLoad of your UICollectionViewController add your element:
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Title" action:@selector(action:)]; [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
Add the following delegate methods:
//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; }
Subclass of UICollectionViewCell, if you haven’t already. Add the method that you specified for your element:
- (void)action:(UIMenuController*)menuController { }
Thus, you do not need any startFirstResponder method or other methods. You have all the actions in one place, and you can easily handle different cells if you call a common method with the cell itself as a parameter.
Edit: somehow uicollectionview needs this method to exist (this method is not called for your custom action, I think uicollectionview just checks for availability)
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { }
Nilz11
source share