UIMenuController:如何判断哪个菜单项被按下?

发布于 2024-10-15 00:44:01 字数 2465 浏览 2 评论 0原文

我在 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   
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-10-22 00:44:01

您可能需要创建一些动态选择器,如 Dynamic UIMenuItems with @ 中所述选择器和动态方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文