最近今天做项目是涉及到不让用户剪切,复制UITextField中的东西,在重写canPerformAction:withSender:方法成功解决:
部分组合选择:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))//禁止粘贴
return NO;
if (action == @selector(select:))// 禁止选择
return NO;
if (action == @selector(selectAll:))// 禁止全选
return NO;
return [super canPerformAction:action withSender:sender];
}
整体禁用:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
特别说明,我是创建了UITextField的子类, 所以此函数一定要在子类中修改,否则修改不生效