关于UIMenuController的使用总结
1. Menu的初始化设置
- (void)showMenuAtRect:(CGRect)rect inView:(UIView *)inView { [self becomeFirstResponder]; UIMenuController *theMenu = [UIMenuController sharedMenuController]; [theMenu setMenuVisible:NO animated:YES]; UIMenuItem *copyItem = [[[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyAction:)] autorelease]; [theMenu setMenuItems:[NSArray arrayWithObjects:copyItem,nil]]; [theMenu setTargetRect:rect inView:inView]; [theMenu setMenuVisible:YES animated:YES]; }
需要注意的是
[self becomeFirstResponder];
一定不能少了
2. Menu的事件响应及显示下面两个方法不能少了,返回Yes
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{ if (action == @selector(copyAction:)) { return YES; } return NO; } -(BOOL)canBecomeFirstResponder{ // 默认是NO return YES; }