UIMenuController:如何判断哪个 menuItem 被按下?

Posted

技术标签:

【中文标题】UIMenuController:如何判断哪个 menuItem 被按下?【英文标题】:UIMenuController: how to tell which menuItem is pressed? 【发布时间】:2011-02-01 10:10:36 【问题描述】:

我在 UITableViewCell 上有一个 UILongPressGestureRecognizer,它显示一个 UIMenuController,其中包含一些用户可以从中选择的类别。这些类别存储在 NSMutableArray 中,并且可以由用户自定义。我想使用一种方法来处理 UIMenuController 中的所有类别按下。如何传递所选 UIMenuItem 的索引?提前致谢。

#pragma mark -
#pragma mark Custom Quick Menu Item

@interface QuickMenuItem : UIMenuItem 




@property (nonatomic, retain) NSIndexPath *indexPath;
@property (nonatomic, retain) NSMutableString *category;

@end

@implementation QuickMenuItem
@synthesize indexPath, category;

- (void)dealloc 

    [indexPath release];
    [category release];
    [super dealloc];


@end

#pragma mark -
#pragma mark Handle UILongPressGesture

- (void)handleLongItemPress:(UILongPressGestureRecognizer *)longPressRecognizer

    if (longPressRecognizer.state == UIGestureRecognizerStateBegan) 
    
        NSIndexPath *pressedIndexPath = [queueTableView indexPathForRowAtPoint:[longPressRecognizer locationInView:queueTableView]];

        if (pressedIndexPath && (pressedIndexPath.row != NSNotFound) && (pressedIndexPath.section != NSNotFound)) 
        
            [self becomeFirstResponder];
               UIMenuController *menuController = [UIMenuController sharedMenuController];
            NSMutableArray *categoryMenuItems = [NSMutableArray array];

            NSEnumerator *e = [self.stats.categories objectEnumerator]; // array with categories
            NSMutableString *cat;
            while (cat = [e nextObject]) 
            
                QuickMenuItem *categoryMenuItem = [[QuickMenuItem alloc] initWithTitle:cat action:@selector(quickMenuCategoryPressed:)];
                categoryMenuItem.indexPath = pressedIndexPath;
                categoryMenuItem.category = cat;
                [categoryMenuItems addObject:categoryMenuItem];
                [categoryMenuItem release];
            

            menuController.menuItems = [NSArray arrayWithArray:categoryMenuItems];
            [menuController setTargetRect:[queueTableView rectForRowAtIndexPath:pressedIndexPath] inView:queueTableView];
            [menuController setMenuVisible:YES animated:YES];
        
    


- (void)quickMenuCategoryPressed:(UIMenuController *)menuController

    QuickMenuItem *menuItem = [[[UIMenuController sharedMenuController] menuItems] objectAtIndex: ??]; // How to tell which category is selected?

    if (menuItem.indexPath) 
    
        [self resignFirstResponder];

        // Perform action   
    

【问题讨论】:

【参考方案1】:

您可能需要创建一些动态选择器,如 Dynamic UIMenuItems with @selector and dynamic methods 所述

【讨论】:

以上是关于UIMenuController:如何判断哪个 menuItem 被按下?的主要内容,如果未能解决你的问题,请参考以下文章

求助:PHP如何判断二维数组哪些竖列的值相同?

在 UITextView 中隐藏 UIMenuController

UIMenuController 和 UITextView 选定的文本

UIMenuController 不显示在第二个 UIViewController

UIMenuController在label中的使用

关闭 UIMenuController 后如何取消选择 UITableViewCell?