2011-02-01 24 views
1

我在UITableViewCell上有一個UILongPressGestureRecognizer,它顯示一個UIMenuController,用戶可以從中選擇一些類別。這些類別存儲在NSMutableArray中,可以由用戶自定義。我想使用一種方法來處理UIMenuController中的所有類別。我怎樣才能傳遞選定的UIMenuItem的索引?提前致謝。UIMenuController:如何判斷哪個menuItem被按下?

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

回答

相關問題