【问题标题】:Enable copy button title on long press on the button长按按钮时启用复制按钮标题
【发布时间】:2020-06-01 02:10:39
【问题描述】:

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

@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://")
        }
    }

【问题讨论】:

    标签: swift xcode button copy-paste street-address


    【解决方案1】:

    使用

    let text =  addressBtn.currentTitle
    

    let text = addressBtn.titleLabel?.text
    

    【讨论】:

    • 然后呢?当您持有时,我如何使复制成为可能?目前它不允许这样做
    【解决方案2】:

    我用下面的代码弄明白了:

    //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)
        }
    

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2020-09-04
      • 2020-10-26
      • 2012-09-29
      • 2018-01-11
      • 2013-05-22
      • 1970-01-01
      • 2019-01-12
      • 2015-07-12
      相关资源
      最近更新 更多