我在集合视图中的单元格内有一个按钮,除了我尝试创建的复制功能外,一切正常。当我单击按钮时,不会复制文本,或者在我的测试用例中,文本不会打印到控制台。
cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonViewLinkAction(sender:UIButton!) {
print("Button tapped")
}
在我的故事板中,我有一个从按钮链接到视图的内部动作(我用ctrl-drag形式创建了这个按钮来查看)。一切看起来还不错,但是当我按下按钮时没有任何反应,当事情看起来没问题时也不会崩溃。
我错过了什么?
答案 0 :(得分:3)
喜欢
复制强>
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)
}
<强>更新-2 强>
您将buttonViewLinkAction
代码写入其永不调用的func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
内。所以删除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
}
答案 1 :(得分:1)
您刚刚发布了代码。问题是你在内部功能。请把它拿出来,它运作良好。
为什么你不这样使用它:
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"
}
答案 2 :(得分:1)
你有两个重复的buttonViewLinkAction功能! 按下按钮时,将调用此功能:
@IBAction func buttonViewLinkAction(sender: UIButton) {
}
而不是
func buttonViewLinkAction(sender:UIButton!) {
print("Button tapped")
}
删除第二个函数并在第一个函数中编写代码。
此外,您不需要写这一行:
cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
如果您已将按钮链接到该功能(在故事板中)。
答案 3 :(得分:0)
更新Swift 4.2
override func viewDidLoad() {
super.viewDidLoad()
label.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(copyValuePressed)))
}
@objc func copyValuePressed() {
UIPasteboard.general.string = label.text
}