简体   繁体   English

使用标准的复制并通过自定义UIMenuController项从UIPasteboard检索值

[英]Use standard Copy and retrieve value from the UIPasteboard with a custom UIMenuController item

i've a web view with displayed a pdf file. 我有一个显示PDF文件的网络视图。 As a standard for the web view the user can select a text and perform standard operation like copy, cut. 作为Web视图的标准,用户可以选择文本并执行标准操作,例如复制,剪切。 I need to add a custom UIMenuController for show custom action. 我需要添加一个自定义UIMenuController来显示自定义动作。 I do not understand how to copy the selected text in the standard UIPasteboard and pass to my custom selector. 我不了解如何在标准UIPasteboard中复制所选文本并将其传递给我的自定义选择器。 I've tried to use 我尝试使用

[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];

in method and i'm able to copy the selected text but i do not understand how to come back this value to my method. 在方法中,我能够复制选定的文本,但是我不明白如何将此值返回到我的方法中。

This is my code (simplified) In the view did load i've added one observer for UIPasteboardChangedNotification for perform my selector myCopy: 这是我的代码(简体),在加载视图时,我为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
 }

Thank you for your help and for your time! 感谢您的帮助和时间!

If I'm understanding your question correctly, then you are trying to grab the text that is currently selected, copy it to the pasteboard when the user taps your custom item, and then do something with that selected/copied text? 如果我正确理解了您的问题,那么您正在尝试获取当前选定的文本,当用户点击您的自定义项目时将其复制到粘贴板,然后对选定的/复制的文本执行某些操作?

If so, I think this should work for you, at least it's working for me in Swift 4. 如果是这样,我认为这应该对您有用,至少在Swift 4中它对我有用。

First, in viewDidLoad , setup your custom menu item 首先,在viewDidLoad ,设置您的自定义菜单项

let customMenuItem = UIMenuItem(title: "Foo Bar", action: #selector(ViewController.customMenuTapped))
UIMenuController.shared.menuItems = [customMenuItem]
UIMenuController.shared.update()

Then, still in viewDidLoad set yourself up as an observer to UIPasteboardChanged 然后,仍然在viewDidLoad将自己设置为UIPasteboardChanged的观察者

NotificationCenter.default.addObserver(self, selector: #selector(displaySelectecText), name: NSNotification.Name.UIPasteboardChanged, object: nil)

Then create your function that responds to the customMenuItem , in my example here it's customMenuTapped() , and you basically send the copy action to the responder chain to handle it. 然后创建响应于customMenuItem函数,在我的示例中,此函数为customMenuTapped() ,然后基本上将复制操作发送到响应程序链以进行处理。

@objc func customMenuTapped() {
    UIApplication.shared.sendAction(#selector(copy(_:)), to: nil, from: self, for: nil)
}

Since you've registered yourself as a listener, when the copy action is done, your other function will get called, mine is displaySelectecText() 由于您已将自己注册为侦听器,因此在完成复制操作后,将调用您的其他函数,其中我的函数是displaySelectecText()

@objc func displaySelectedText() {
    let copiedText = UIPasteboard.general.string ?? ""
    let alertView = UIAlertController(title: "Selected Text", message: copiedText, preferredStyle: .alert)
    alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    present(alertView, animated: true, completion: nil)
}

In this example, I am just displaying the text in an alert view for simplicity. 在此示例中,为简单起见,我仅在警报视图中显示文本。

Here is the complete code: 这是完整的代码:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        loadWebView()
        setupCustomMenu()
        setupListener()
    }

    func loadWebView() {
        let url = URL(string: "http://www.google.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func setupCustomMenu() {
        let customMenuItem = UIMenuItem(title: "Foo Bar", action:
            #selector(ViewController.customMenuTapped))
        UIMenuController.shared.menuItems = [customMenuItem]
        UIMenuController.shared.update()
    }

    func setupListener() {
        NotificationCenter.default.addObserver(self, selector: #selector(displaySelectedText), name: NSNotification.Name.UIPasteboardChanged, object: nil)
    }

    @objc func customMenuTapped() {
        UIApplication.shared.sendAction(#selector(copy(_:)), to: nil, from: self, for: nil)
    }

    @objc func displaySelectedText() {
        let copiedText = UIPasteboard.general.string ?? ""
        let alertView = UIAlertController(title: "Selected Text", message: copiedText, preferredStyle: .alert)
        alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alertView, animated: true, completion: nil)
    }

}

And here is a demo 这是一个演示

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM