I am trying to use UIMenuCnotroller
to show a list of dynamically generated items, they share the same action method, and so I need to know which item is selected in the single action method.
However, in the action method - (void)menuItemAction:(id)sender;
the sender is actually the UIMenuController
object, and I didn't find any method of UIMenuController
can tell me which menuitem is selected.
One solution I can think of is to dynamically generate different action selectors for different items, and do some tricks in forwardInvocation
But is there any easier way?
You can use UIMenuCnotroller like: 1) creation:
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *open = [[UIMenuItem alloc] initWithTitle:@"Open" action:@selector(open:)];
UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:@"Re-Download" action:@selector(reDownload:)];
[menuController setMenuItems:[NSArray arrayWithObjects:open, reDownload, nil]];
[menuController setTargetRect:cell.frame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[open release];
[reDownload release];
2) To catch actions should implement next methods:
- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender
{
if (selector == @selector(open:))
{
return YES;
}
if (selector == @selector(reDownload:))
{
return YES;
}
return NO;
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
3) And realization of yours methods:
- (void) open:(id) sender
{
[self doSomething];
}
- (void) reDownload:(id) sender
{
[self doSomething];
}
Hope, this helps.
One easiest way would be to use different @selector
method for each menu item
Examples:
UIMenuItem *oneObj = [[UIMenuItem alloc] initWithTitle:@"One" action:@selector(One:)];
UIMenuItem *twoObj = [[UIMenuItem alloc] initWithTitle:@"Two" action:@selector(Two:)];
Okay, I've solved this one. It involves messing with [NSObject forwardInvocation:] and is a bit dirty, but the resulting code is quite minimal. Answered over here: https://stackoverflow.com/a/9874092/790036
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.