gpt4 book ai didi

ios - Swift 错误,UIMenuItem 到 UIview/UIlabel 进行复制

转载 作者:行者123 更新时间:2023-11-29 00:58:21 25 4
gpt4 key购买 nike

我在从 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. Copyable Label Tutorial Swift

  2. GitHub CopyLabel Class swift

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

希望对你有帮助:)

关于ios - Swift 错误,UIMenuItem 到 UIview/UIlabel 进行复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37345618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com