Reputation: 4549
I am having an issue in finding the UIview/Casting from UImenuItem and UILongPressGestureRecognizer. I can see the copy button but once i click i am having an error of casting.
Below is My Code
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
}
ERROR BELOW
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
Could someone help me to find out solutions for below?
Upvotes: 0
Views: 763
Reputation: 999
Look at some tutorials and GitHub project below
And if you want to use some existing Objective C code into Swift code then use This site to convert Objective c to swift. it dose a good job.
Hope it help :)
Upvotes: 1