Pass value through UIMenuItem of UIMenuController
ios, objective-c, uimenucontroller
Solution
So simple, just create a class of type `UIMenuItem`, add property in it, and use your `UIMenuItem class` instead of actual `UIMenuItem`. See how.
Create a class say `MyMenuItem` of `type UIMenuItem`.
`MyMenuItem.h`
#import <UIKit/UIKit.h>
@interface MyMenuItem : UIMenuItem
@property(nonatomic, strong)NSIndexPath *indexPath;
@end
`MyMenuItem.m`
#import "MyMenuItem.h"
@implementation MyMenuItem
@end
And then
{
MyMenuItem *deleteMenuItem = [[MyMenuItem alloc] initWithTitle:@"Delete" action:@selector(numberDelete:)];
deleteMenuItem.indexPath=indexPath;//Assign to property
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:deleteMenuItem, nil]];
[menu setTargetRect:[self.pullTableView rectForRowAtIndexPath:indexPath] inView:self.pullTableView];
[menu setMenuVisible:YES animated:YES];
}
-(void)numberDelete:(id)sender {
//receive value of row here. The sender in iOS 7 is an instance of UIMenuController.
UIMenuController *targetSender = (UIMenuController *)sender ;
MyMenuItem *menuItem=(MyMenuItem *)[targetSender.menuItems firstObject];
NSLog(@"%d",menuItem.indexPath.row);
}
I hope it helps.
Cheers.
Problem
I am using following method to show menu on long press in UITableViewCell. I have need to pass a value pressing Delete menu Item to -(void)numberDelete method. ``` -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { if(gestureRecognizer.state == UIGestureRecognizerStateBegan) { CGPoint p = [gestureRecognizer locationInView: self.pullTableView]; NSIndexPath *indexPath = [self.pullTableView indexPathForRowAtPoint:p]; if(indexPath != nil) { [self becomeFirstResponder]; NSInteger *row = indexPath.row; //need to pass this row value through @selector(numberDelete:) UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(numberDelete:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:delete, nil]]; [menu setTargetRect:[self.pullTableView rectForRowAtIndexPath:indexPath] inView:self.pullTableView]; [menu setMenuVisible:YES animated:YES]; } } } -(void)numberDelete:(id)sender { //receive value of row here } -(BOOL)canBecomeFirstResponder { return YES; } -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(customDelete:) ){ return YES; } return NO; } ```