【问题标题】:Swift button press copy text快速按钮按下复制文本
【发布时间】:2019-07-11 22:49:29
【问题描述】:

我在集合视图的单元格内有一个按钮,除了我试图创建的复制功能外,一切正常。当我单击按钮时,文本不会被复制,或者在我的测试用例中,文本不会打印到控制台。

    cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

    func buttonViewLinkAction(sender:UIButton!) {
        print("Button tapped")

    }

在我的情节提要中,我有一个从按钮链接到视图的内部操作(我使用 ctrl-drag 形成要查看的按钮创建此操作)。一切看起来都不错,但是当我按下按钮时,什么也没有发生,也没有崩溃,因为一切看起来都不错。

我错过了什么?

【问题讨论】:

  • 是打印还是不打印("Button tapped")
  • 能否显示一些额外的代码
  • @IBAction func buttonViewLinkAction(sender: UIButton) { } 这是 IBAction 你还想看什么?
  • cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal) cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20) cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside) func buttonViewLinkAction(sender:UIButton!) { print("Button tapped") 完整块,我正在使用 FontAwesome 来绘制按钮,不要认为这很重要。

标签: ios swift xcode


【解决方案1】:

喜欢

复制

 func buttonViewLinkAction(sender:UIButton!) {
        print("Button tapped")
       UIPasteboard.generalPasteboard().string = yourstring!.text() // or use  sender.titleLabel.text

    }

粘贴或检索

func GetCopiedText(sender: UIButton!) {

    if let myString = UIPasteboard.generalPasteboard().string {
        print(myString)
    }

}

更新

 func buttonViewLinkAction(sender:UIButton!) {
        print(sender.currentTitle)
         print(sender.titleLabel.text)
 }

update-2

您在func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 中编写了buttonViewLinkAction 代码,它从不调用。所以删除buttonViewLinkAction并在数据源方法之外添加

 //fontawesome link
    cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
    cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
    cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

    func buttonViewLinkAction(sender:UIButton!) {
        print("Button tapped")
        UIPasteboard.generalPasteboard().string = "Label text"

    }

最终答案

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellClass
    let face = self.faces[indexPath.item]

    //set image and align center
    if let imageURL = NSURL(string:face.image) {
        cell.imageView.sd_setImageWithURL(imageURL)

    } else {
        cell.imageView.image = self.placeholderImage
    }

    //set name
    if let imageNAME: String = String(face.name){
        cell.labelView.text = (imageNAME .uppercaseString)

    } else {
        cell.labelView.text = "oops name"
    }

    //set border
    cell.layer.shadowOffset = CGSizeMake(0, 1)
    cell.layer.shadowColor = UIColor.blackColor().CGColor
    cell.layer.shadowRadius = 1
    cell.layer.shadowOpacity = 0.1
    cell.clipsToBounds = false
    let shadowFrame: CGRect = (cell.layer.bounds)
    let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
    cell.layer.shadowPath = shadowPath

    //square background button
    cell.buttonViewSquare.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1)
    cell.buttonViewSquare.enabled = false

    //fontawesome link
    cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
    cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
    cell.buttonViewLink.addTarget(self, action: "buttonViewLink:", forControlEvents: UIControlEvents.TouchUpInside)
    // or use like    cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)




    //fontawesome heart
    cell.buttonViewHeart.setTitle(String.fontAwesomeIconWithName(.Heart), forState: .Normal)
    cell.buttonViewHeart.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
    cell.buttonViewHeart.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

    //fontawesome share
    cell.buttonViewShare.setTitle(String.fontAwesomeIconWithName(.Share), forState: .Normal)
    cell.buttonViewShare.titleLabel?.font = UIFont.fontAwesomeOfSize(20)




    return cell
}

调用方法如

  func buttonViewLink(sender:UIButton!) {
        print("Button tapped")
        UIPasteboard.generalPasteboard().string = sender.titleLabel.text

    }

或者直接使用已有的功能

  @IBAction func buttonViewLinkAction(sender: UIButton) {
         print("Button tapped")
        UIPasteboard.generalPasteboard().string = sender.titleLabel.text
  }

修改后的答案

在此处添加标签cell.buttonViewLink.tag = indexPath.item

     cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
    cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
    cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
         cell.buttonViewLink.tag = indexPath.item

然后像这样调用方法

@IBAction func buttonViewLinkAction(sender: UIButton) {
         print("Button tapped")
        let face = self.faces[sender.tag]
         if let imageNAME: String = String(face.name){
           print(imageNAME .uppercaseString)
           }
           if let imageURL = NSURL(string:face.image) {
            print(imageURL)

           }
        UIPasteboard.generalPasteboard().string = sender.titleLabel.text
  }

【讨论】:

  • 这就是我最初所做的,当我注意到它不起作用时,我恢复到只使用 Print 进行测试。这两个例子都不起作用,我不知道我做错了什么。没有错误,没有崩溃。我点击按钮,没有任何反应 - 没有打印或复制。
  • 启用断点并检查按钮操作是否被调用
  • 否则附上您的项目,我们将检查
  • @user3591436 - 你犯了错误
  • Anbu 确实有效,但是如果我将函数放在单元格之外,我将无法获取 imageURL 和 imageNAME 之类的变量,这两个变量是从 json 存储的,我需要在点击时复制它们。
【解决方案2】:

您刚刚发布了您的代码。问题是你在函数内部做。请把它拿出来,它工作得很好。

为什么你不这样使用它:

cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

func buttonViewLinkAction(sender:UIButton!) {
    print("Button tapped")
    UIPasteboard.generalPasteboard().string = "Label text"

}

【讨论】:

  • 因为这是我最初所做的,当我注意到复制功能不起作用时,我又恢复为打印,因为这样更容易测试。
  • 所以你的问题是即使点击它也不能打印?
  • 正确,不是打印或复印。都不行。
  • 如果你点击按钮,它们是否突出显示?
  • 是的,我可以看到单击,当我单击然后取消单击时,按钮的颜色会发生变化。
【解决方案3】:

您有两个重复的 buttonViewLinkAction 函数! 当您按下按钮时,将调用此函数:

@IBAction func buttonViewLinkAction(sender: UIButton) {
}

而不是这个

func buttonViewLinkAction(sender:UIButton!) {
    print("Button tapped")
}

删除第二个函数并在第一个函数中编写代码。

另外,你不需要写这行:

cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

如果您已经将按钮链接到功能(在故事板中)。

【讨论】:

  • 如果您以编程方式添加目标,则无需创建 @IBAction 只需 func 即可。但是,如果您想将情节提要中的动作连接到该特定按钮,则只需创建 @IBAction
【解决方案4】:

更新 Swift 4.2

override func viewDidLoad() {
    super.viewDidLoad()
    label.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(copyValuePressed)))
}

@objc func copyValuePressed() {
    UIPasteboard.general.string = label.text
}

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 2017-02-06
    • 2016-09-02
    • 2018-07-30
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多