- 默认情况下, 有以下控件已经支持UIMenuController - UITextField - UITextView - UIWebView
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if { (action == @selector(cut:)
|| action == @selector(copy:)
|| action == @selector(paste:))
return YES;
}
return NO;
}
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
- (void)cut:(UIMenuController *)menu {
[self copy:menu];
self.text = nil;
}
- (void)copy:(UIMenuController *)menu {
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = self.text;
}
- (void)paste:(UIMenuController *)menu {
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
}
UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)];
UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(replay:)];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];
menu.menuItems = @[ding, replay, report];
注意:所实现的方法要写在控制器中