苹果官方提供的 菜单栏控制器的选择 (需要注意的就是让他成为第一响应者)
- (void)viewDidLoad {
[super viewDidLoad];
_wlLabel.userInteractionEnabled = YES;
[_wlLabel addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)longTap:(UILongPressGestureRecognizer *)tap {
//成为第一响应者
[self becomeFirstResponder];
if (tap.state == UIGestureRecognizerStateBegan) {
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(item)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(select)];
UIMenuItem *item3 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(shear)];
UIMenuController *menVC = [UIMenuController sharedMenuController];
[menVC setMenuItems:@[item1,item2,item3]];
[menVC setTargetRect:_wlLabel.frame inView:_wlLabel.superview];
[menVC setMenuVisible:YES animated:YES];
}
}
- (BOOL)canBecomeFirstResponder {
//明确该控件可以成为第一响应者
return YES;
}
//赋值
- (void)item {
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = _wlLabel.text;
}
//删除
- (void)select {
_wlLabel.text = nil;
}
//剪切
- (void)shear {
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = _wlLabel.text;
_wlLabel.text = @"";
}