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
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要创建一些动态选择器,如 Dynamic UIMenuItems with @ 中所述选择器和动态方法
You'll probably need to create some dynamic selectors, as described at Dynamic UIMenuItems with @selector and dynamic methods