简体   繁体   English

Custom UIMenuController using button event error 只能有一个 UIMenuController 实例

[英]Custom UIMenuController using button event error There can only be one UIMenuController instance

I create a button that when you hold, it will show the standard ios menu button for pasting a text, but I'm getting an error saying There can only be one UIMenuController instance.我创建了一个按钮,当您按住该按钮时,它将显示用于粘贴文本的标准 ios 菜单按钮,但我收到一条错误There can only be one UIMenuController instance. when I hold the button 2 times, how can I fix this?当我按住按钮 2 次时,我该如何解决这个问题?

Here is my code这是我的代码

override init(frame: CGRect) {
    super.init(frame: frame)
    self.configureView()
}
    
required init?(coder: NSCoder) {
    super.init(coder: coder)
    self.configureView()
}

private func configureView() {
    guard let view = self.loadViewFromNib(nibName: "CustomView") else { return }
    view.frame = self.bounds
    self.addSubview(view)
        
    button.addTarget(self, action: #selector(holdButton), for: .touchDown)
}

@objc func holdButton(_ sender: UIButton) {
    let menuController = UIMenuController()
    menuController.setTargetRect(sender.frame, in: charTextField)
    menuController.setMenuVisible(true, animated: true)
}

Also, how can I listen to the user when he clicked the paste button?另外,当用户单击paste按钮时,我如何倾听用户的声音?

I want it to call this function when he clicked paste .当他单击paste时,我希望它调用此 function 。

func pasteClick() {
    print("pasted", clipboardString())
}

Use the default singleton instance provided ( .shared ) by UIMenuController instead of creating an instance of your own.使用 UIMenuController 提供的默认UIMenuController实例 ( .shared ),而不是创建您自己的实例。

@objc func holdButton(_ sender: UIButton) {
    UIMenuController.shared.setTargetRect(sender.frame, in: charTextField)
    UIMenuController.shared.setMenuVisible(true, animated: true)
}

Quoting from apple doc :引用苹果文档

The singleton UIMenuController instance is referred to as the editing menu..... singleton UIMenuController 实例简称为编辑菜单.....

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

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