通过UIMenuController的UIMenuItem传递值 [英] Pass value through UIMenuItem of UIMenuController
本文介绍了通过UIMenuController的UIMenuItem传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下方法在UITableViewCell中长按显示菜单.
I am using following method to show menu on long press in UITableViewCell.
我需要通过按Delete菜单项将值传递给-(void)numberDelete方法.
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;
}
推荐答案
如此简单,只需创建类型为UIMenuItem
的类,在其中添加属性,然后使用您的UIMenuItem class
而不是实际的UIMenuItem
.看看如何.
So simple, just create a class of type UIMenuItem
, add property in it, and use your UIMenuItem class
instead of actual UIMenuItem
. See how.
创建一个类,例如type UIMenuItem
的MyMenuItem
.
MyMenuItem.h
MyMenuItem.h
#import <UIKit/UIKit.h>
@interface MyMenuItem : UIMenuItem
@property(nonatomic, strong)NSIndexPath *indexPath;
@end
MyMenuItem.m
MyMenuItem.m
#import "MyMenuItem.h"
@implementation MyMenuItem
@end
然后
{
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);
}
希望对您有帮助.
干杯.
这篇关于通过UIMenuController的UIMenuItem传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文