Swift Error, UIMenuItem to UIview/UIlabel for Copy



我在从UImenuItem和UILongPressGestureRecognizer查找UIview/Casting时遇到问题。我可以看到复制按钮,但是一旦单击,我就遇到了铸造错误。

下面是我的代码

ViewDidLoad

 let copyLongPress = UILongPressGestureRecognizer(target: self, action: #selector(OfficeMapController.handleLongPress(_:)))
 self.addressView.addGestureRecognizer(copyLongPress)

   func handleLongPress(longPressView :UILongPressGestureRecognizer) {
    becomeFirstResponder()
    let menu = UIMenuController.sharedMenuController()
    let copyItem = UIMenuItem(title: "Copy", action:  #selector(OfficeMapController.copyText))
           menu.menuItems = [copyItem]
    menu.setTargetRect(CGRectMake(50, 50, 50, 50), inView: longPressView.view!)
    menu.setMenuVisible(true, animated: true)

}
  override func canBecomeFirstResponder() -> Bool {
    return true
}
  func copyText(sender: UILongPressGestureRecognizer)
{
    let searchlbl = sender.view! as UIView
    print(searchlbl)
    //Than Label Value code for Copy
}
 override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    // You need to only return true for the actions you want, otherwise you get the whole range of
    //  iOS actions. You can see this by just removing the if statement here.
    if action == #selector(OfficeMapController.copyText) {
        return true
    }
    return false
}

下面的错误

  2016-05-20 16:59:40.428 [2732:1548168] -[UIMenuController view]:     unrecognized selector sent to instance 0x155507820
 2016-05-20 16:59:40.429 [2732:1548168] *** Terminating app due to uncaught    exception 'NSInvalidArgumentException', reason: '-[UIMenuController view]:    unrecognized selector sent to instance 0x155507820'
  *** First throw call stack:
  (0x180c3ae38 0x18029ff80 0x180c41ccc 0x180c3ec74 0x180b3cd1c 0x100183dbc    0x100183f10 0x18634638c 0x18634574c 0x181602628 0x180bf181c 0x180bf14c0   0x180beebd4 0x180b18d10 0x182400088 0x185dedf70 0x10019caa0 0x1806b68b8)
  libc++abi.dylib: terminating with uncaught exception of type NSException

有人可以帮助我找出以下解决方案吗?

看看下面的一些教程和 GitHub 项目

  1. 可复制标签教程 Swift

  2. GitHub CopyLabel Class swift

如果你想使用一些现有的 Objective C 代码到 Swift 代码中,那么请使用这个网站将 Objective c 转换为 swift。

希望对:)有所帮助

最新更新