Pass value through UIMenuItem of UIMenuController

天大地大妈咪最大 提交于 2019-12-07 08:56:58

问题


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;
}

回答1:


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.




回答2:


Modified version of the accepted answer in Swift 4

MenuItemWithIndexPath.swift:

class MenuItemWithIndexPath: UIMenuItem {
    var indexPath: IndexPath?

    init(title: String, action: Selector, indexPath: IndexPath) {
        super.init(title: title, action: action)
        self.indexPath = indexPath
    }
}

Usage:

let menu = UIMenuController.shared
menu.menuItems = [MenuItemWithIndexPath(title: "Delete", action: #selector(numberDelete(sender:)), indexPath: indexPath)]
menu.setTargetRect(tableView.rectForRow(at: indexPath), in: tableView)
menu.setMenuVisible(true, animated: true)


@objc func numberDelete(sender:UIMenuController) {
    if let menuItem = sender.menuItems?.first as? MenuItemWithIndexPath,
        let indexPath = menuItem.indexPath {
        print("delete at indexPath: \(indexPath)")
    }
}


来源:https://stackoverflow.com/questions/24729707/pass-value-through-uimenuitem-of-uimenucontroller

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