使用UIPasteboard中的标准复制和检索值以及自定义UIMenuController项

时间:2012-05-30 15:07:13

标签: ios uimenucontroller uipasteboard

我有一个显示pdf文件的Web视图。作为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
 }

感谢您的帮助和时间!

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,那么您尝试抓取当前所选的文本,在用户点按您的自定义项目时将其复制到粘贴板,然后对所选/复制的文本执行某些操作?

如果是这样,我认为这对你有用,至少它在Swift 4中为我工作。

首先,在viewDidLoad中,设置自定义菜单项

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

然后,仍然在viewDidLoad将自己设置为UIPasteboardChanged

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

然后创建响应customMenuItem的函数,在我的示例中customMenuTapped(),您基本上将复制操作发送到响应程序链来处理它。

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

由于您已将自己注册为听众,因此当复制操作完成后,您的其他功能将被调用,我的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)
}

在这个例子中,为了简单起见,我只是在警告视图中显示文本。

以下是完整的代码:

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)
    }

}

这是一个演示

enter image description here