0

我在从 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

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

4

1 回答 1

1

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

  1. 可复制标签教程 Swift

  2. GitHub CopyLabel 类 swift

如果您想将一些现有的 Objective C 代码转换为 Swift 代码,请使用此站点将 Objective c 转换为 swift。它做得很好。

希望它有帮助:)

于 2016-05-20T13:52:41.673 回答