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

时间:2011-02-01 10:10:37

标签: objective-c uimenucontroller

我在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 个答案:

答案 0 :(得分:0)

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

中所述