删除"复制"以编程方式创建的菜单项UITextView IOS

时间:2015-05-20 08:34:39

标签: ios uitextview uimenuitem

我在viewDidLoad中以编程方式创建了UITextView。
当我选择文本时,菜单显示以下内容:

enter image description here

如图所示,我添加了两个自定义按钮,高亮和不亮。我想删除"复制"选项并保留所有其他,所以我不能使它不可编辑,我需要允许用户从文本中选择他想要的任何内容,但防止它复制内容。
我尝试了几种方法,包括所有社区所指的方法:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
     NSLog(@"it went in canPerform");
     if (action == @selector(copy:)){

          NSLog(@"It prevented the copy option and hid it from the menu");
          return NO;

     }
     return [super canPerformAction:action withSender:sender];
}

为了更好地了解问题,我试图注意选择器是否实际考虑了"复制"选项,这是我的日志:

2015-05-20 11:27:47.637 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.637 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.663 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

如图所示,代码正在调用" canPerformAction"因为日志显示"它进入了canPerform"但它没有识别" copy"动作,因为日志从未显示:"它阻止了复制选项并将其隐藏在菜单中#34;。
因此,我的问题是我需要隐藏"复制"所选文本菜单中的项目。我错过了什么?

3 个答案:

答案 0 :(得分:2)

对于所有未来需要处理这种情况的开发人员,我知道你会感到多么沮丧,所以我会给出一个完整的教程,因为我找到了结合大多数已发布答案的正确答案:

1 - 我 t仅在以编程方式创建UITextView时才有效,如果您使用故事板创建它,它将无法工作,这是最重要的部分,因为如果您已经有使用故事板创建了UITextView,您无法删除" copy" ,你将能够删除其他一切!只是没有复制

2 - 您必须为所有新开发人员创建UITextView类的子类,这可以通过创建一个新类并选择它作为UITextView的子类来完成。

3 - 你的.h文件看起来像那样

//
//  myCustomClass.h
//  myApp
//
//  Created by Elias Rahme on 5/20/15.
//  Copyright (c) 2015 Elias Rahme. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface myCustomClass : UITextView

@end

你的.m文件就是这样:

//
//  myCustomClass.m
//  myApp
//
//  Created by Elias Rahme on 5/20/15.
//  Copyright (c) 2015 Elias Rahme. All rights reserved.
//

#import "myCustomClass.h"

@implementation myCustomClass


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
     if (action == @selector(copy:)) {
          return NO;
     }
     return [super canPerformAction:action withSender:sender];
}
@end

4 - 现在设置完所有之后,这里有一个棘手的部分。在您想要使用UITextView的类中,当然包括您的customClass,然后,这就是您调用textView的方式:

首先,您必须创建一个新的UITextView,这可以通过在实现上方写UITextView *newTextView;来完成,就像创建任何变量一样。

其次,在你的实际课程中,你已完成所有工作,转到你的.h文件,并确保它看起来像那样

#import <UIKit/UIKit.h>

@interface myActuallClassWhereIWantTheActualWorkToBeDone : UIViewController< UITextViewDelegate>{


}

第三,将以下行添加到viewDidLoad方法中:

 newTextView = [[myCustomClass alloc] initWithFrame:CGRectMake(yourX, yourY, yourWidth, yourHeight)];
newTextView.text = @"This is the best small tutorial EVER!!";
 newTextView.delegate = self;

然后你去,运行你的应用程序,你就不会看到&#34; copy&#34;选项!这是一个简单而棘手的程序,我花了几个小时来实际总结我访问过的所有网页!希望大家都喜欢它!

答案 1 :(得分:0)

canPerformAction(_:withSender:)是一个UIResponder方法,所以如果你在viewController中实现它,它只会在viewController上调用copy:时触发。文档说:"Requests the receiving responder to enable or disable the specified command in the user interface.

你必须继承UITextView并在子类中实现它,我已经为你创建了一个简单的gist

答案 2 :(得分:-1)

尝试这个

UIMenuItem *textItem1 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Start", nil) action:@selector(start:)];
UIMenuItem *textItem2 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Reply", nil) action:@selector(reply:)];

UIMenuItem *textItem3 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Report Abuse", nil) action:@selector(startreport:)];

[UIMenuController sharedMenuController].menuItems = @[textItem1, textItem2, textItem3];



[[UIMenuController sharedMenuController] setTargetRect:cell.imgBg.frame inView:cell.contentView];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];