本文介绍了UIMenuController:如何知道按下哪个menuItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell上有一个UILongPressGestureRecognizer,它显示UIMenuController以及用户可以从中选择的某些类别.这些类别存储在NSMutableArray中,并且可以由用户自定义.我想使用一种方法来处理UIMenuController中的所有类别按.如何传递所选UIMenuItem的索引?预先感谢.

I have a UILongPressGestureRecognizer on a UITableViewCell that displays a UIMenuController with some categories the user can pick from. These categories are stored in a NSMutableArray and can be customized by the user. I want to use one method to handle all category-presses in the UIMenuController. How can I pass the index of the selected UIMenuItem? Thanks in advance.

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

推荐答案

您可能需要创建一些动态选择器,如

You'll probably need to create some dynamic selectors, as described at Dynamic UIMenuItems with @selector and dynamic methods

这篇关于UIMenuController:如何知道按下哪个menuItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:06