通过以下设置
.... 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]; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。