我有一个显示 pdf 文件的 web 视图。作为网络视图的标准,用户可以选择文本并执行标准操作,如复制、剪切。我需要添加一个自定义 UIMenuController 来显示自定义操作。我不明白如何在标准 UIPasteboard 中复制所选文本并传递给我的自定义选择器。我试过用
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
在方法中,我可以复制选定的文本,但我不明白如何将此值返回到我的方法中。
这是我的代码(简化) 在视图中确实加载了我为 UIPasteboardChangedNotification 添加了一个观察者来执行我的选择器 myCopy:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myCopy:) name:UIPasteboardChangedNotification object:nil];
UIMenuItem *schedaMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"POPSCHEDA",nil) action:@selector(copyScheda:)];
UIMenuItem *istruzionetMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"POPISTR",nil) action:@selector(copyIstruzione:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:MenuItem1,MenuItem2,nil]];
}
-(void)myCopy:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString* copyCode = pasteboard.string;
//perform the necessary action
}
-(void)copyScheda:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString* copyCode = pasteboard.string;
//perform the necessary action
}
-(void)copyIstruzione:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString* copyCode = pasteboard.string;
//perform the necessary action
}
感谢您的帮助和时间!