通过以下设置
....
MyUIMenuItem *someAction = [[MyUIMenuItem alloc]initWithTitle : @"Something" action : @selector(menuItemSelected:)];
MyUIMenuItem *someAction2 = [[MyUIMenuItem alloc]initWithTitle : @"Something2" action : @selector(menuItemSelected:)];
....
- (IBAction) menuItemSelected : (id) sender
{
UIMenuController *mmi = (UIMenuController*) sender;
}
如何确定选择了哪个菜单项.
并且不要说你需要有两种方法……提前谢谢.
解决方法
好的,我已经解决了这个问题.解决方案并不漂亮,更好的选择是“Apple修复问题”,但这至少有效.
首先,在UIMenuItem动作选择器前加上“magic_”.并且不要制作相应的方法. (如果你能做到这一点,那么你无论如何都不需要这个解决方案).
我正在构建我的UIMenuItems:
NSArray *buttons = [NSArray arrayWithObjects:@"some",@"random",@"stuff",nil];
NSMutableArray *menuItems = [NSMutableArray array];
for (Nsstring *buttonText in buttons) {
Nsstring *sel = [Nsstring stringWithFormat:@"magic_%@",buttonText];
[menuItems addobject:[[UIMenuItem alloc]
initWithTitle:buttonText
action:NSSelectorFromString(sel)]];
}
[UIMenuController sharedMenuController].menuItems = menuItems;
现在,您的班级抓住按钮点击消息需要一些补充. (在我的例子中,该类是UITextField的子类.你的可能是其他东西.)
首先,我们一直希望拥有但不存在的方法:
- (void)tappedMenuItem:(Nsstring *)buttonText {
NSLog(@"They tapped '%@'",buttonText);
}
然后使方法成为可能:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
Nsstring *sel = NsstringFromSelector(action);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
return YES;
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
if ([super methodSignatureForSelector:sel]) {
return [super methodSignatureForSelector:sel];
}
return [super methodSignatureForSelector:@selector(tappedMenuItem:)];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
Nsstring *sel = NsstringFromSelector([invocation selector]);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
[self tappedMenuItem:[sel substringFromIndex:6]];
} else {
[super forwardInvocation:invocation];
}
}