长按按钮启用复制按钮标题

Enable copy button title on long press on the button

我的 tableview 单元格中有一个地址的 UIButton。当我点击它一次时;我用方向打开google地图没问题。现在,我想提供长手势选项,如果您将手指放在按钮上,它会提供复制按钮标题中地址的选项。这是我的代码:

@IBOutlet weak var addressBtn: UIButton!


override func awakeFromNib() {
    super.awakeFromNib()

    addLongPressGesture()
}

@objc func longPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == UIGestureRecognizer.State.began {

        // how do I make it possible to copy the title of the button here? The address is already inserted as the title of the button

    }
}

func addLongPressGesture(){
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
    longPress.minimumPressDuration = 0.5
    self.addressBtn.addGestureRecognizer(longPress)
}

这是点击一下就可以毫无问题地转到地图的地方;所以我在这里没有问题,但仅供参考: @IBAction func addressClicked(_ sender: Any) {

    if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {

        let street = order.street.replacingOccurrences(of: " ", with: "+")
        let postalCode = order.postalCode.replacingOccurrences(of: " ", with: "+")

        if street == "" || order.city == "" || order.province == "" || postalCode == ""{
            UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=\(order.longitude),\(order.latitude)&directionsmode=driving")! as URL)
        } else {
            UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=+\(street),+\(order.city),+\(order.province),+\(postalCode)&directionsmode=driving")! as URL)
        }

        } else {
            NSLog("Can't use comgooglemaps://")
        }
    }

使用

let text =  addressBtn.currentTitle

let text = addressBtn.titleLabel?.text

我用下面的代码解决了这个问题:

//Create the AlertController and add Its action like button in Actionsheet
        let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    actionSheetControllerIOS8.view.tintColor = AppColors.Blue

    let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
    }
    actionSheetControllerIOS8.addAction(cancelActionButton)

    let saveActionButton = UIAlertAction(title: "Open Google Map", style: .default)
    { _ in

        if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {

            let street = order.street.replacingOccurrences(of: " ", with: "+")
            let postalCode = order.postalCode.replacingOccurrences(of: " ", with: "+")

            if street == "" || order.city == "" || order.province == "" || postalCode == ""{
                UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=\(order.longitude),\(order.latitude)&directionsmode=driving")! as URL)
            } else {
                UIApplication.shared.open(URL(string:"comgooglemaps://?saddr=&daddr=+\(street),+\(order.city),+\(order.province),+\(postalCode)&directionsmode=driving")! as URL)
            }

        } else {
            NSLog("Can't use comgooglemaps://")
        }
    }
    actionSheetControllerIOS8.addAction(saveActionButton)

    let deleteActionButton = UIAlertAction(title: "Copy Address", style: .default)
    { _ in

        let address = "\(order.street), \(order.city), \(order.province), \(order.postalCode)"
        let pasteBoard = UIPasteboard.general
        pasteBoard.string = address

    }
    actionSheetControllerIOS8.addAction(deleteActionButton)
    self.present(actionSheetControllerIOS8, animated: true, completion: nil)
    }