Включить копирование заголовка кнопки при долгом нажатии на кнопку

У меня есть UIButton для адреса в моей ячейке tableview. Когда я нажимаю на нее один раз; Я открываю карту 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)
}

Здесь одним касанием он без проблем попадает на карту; так что у меня нет проблем, но просто fyi: @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://")
        }
    }

person user12669401    schedule 01.06.2020    source источник


Ответы (2)


Использовать

let text =  addressBtn.currentTitle

or

let text = addressBtn.titleLabel?.text
person Sh_Khan    schedule 01.06.2020
comment
И что? Как сделать так, чтобы можно было копировать при удержании? В настоящее время это не позволяет - person user12669401; 01.06.2020

Я понял это с помощью следующего кода:

//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)
    }
person user12669401    schedule 01.06.2020