如何在 Swift 中知道发件人的标识符 [英] How to know the sender's identifier in Swift

查看:21
本文介绍了如何在 Swift 中知道发件人的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 中有两个 UILabels 和两个 UITapGestureRecognizers.

I have two UILabels with two UITapGestureRecognizers in a UITableViewCell.

cell.Username.tag = indexPath.row
cell.SharedUser.tag = indexPath.row
let tapGestureRecognizer2 = UITapGestureRecognizer(target:self, action:"GoToProfil:")
let tapGestureRecognizer3 = UITapGestureRecognizer(target:self, action:"GoToProfil:")
cell.Username.userInteractionEnabled = true
cell.Username.addGestureRecognizer(tapGestureRecognizer2)
cell.SharedUser.userInteractionEnabled = true
cell.SharedUser.addGestureRecognizer(tapGestureRecognizer3)

func GoToProfil (sender: AnyObject!) {
    self.performSegueWithIdentifier("GoToProfilSegue", sender: sender)
}

我正在使用 Segue 来推送另一个 UIViewController,并且我正在覆盖 PrepareSegue 函数以发送对应的所需信息Sender 标签.

I'm using a Segue to push another UIViewController, and I'm overriding the PrepareSegue function to send the needed information corresponding to the Sender tag.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    let ProfilView = segue.destinationViewController as! Profil
    ProfilView.hidesBottomBarWhenPushed = true
    ProfilView.title = posts[sender.view!.tag].User?.objectForKey("Name") as? String
    ProfilView.User = posts[sender.view!.tag].User
}

我的问题是我想知道按下了哪个 UILabel,因为我已经在使用 tag.

My problem is that I want to know which UILabel was pressed, knowing that I'm already using tag.

推荐答案

您的 GoToProfile: 函数应该正确编写.参数不是发送者",而是手势识别器.

Your GoToProfile: function should be written properly. The parameter isn't the "sender", it's the gesture recognizer.

func GoToProfil (gestureRecognizer: UITapGestureRecognizer) {
}

从那里,您可以使用手势识别器的 view 属性来确定标签.

From there, you can determine the label by using the view property of the gesture recognizer.

但是您似乎有两个相互矛盾的要求.您想知道点击了两个标签中的哪一个,以及标签在哪一行.

But you seem to have two conflicting requirements. You want to know which of the two labels was tapped and you want to know which row the label is in.

通常,您会使用标签的 tag 来了解点击了两个标签中的哪一个.但是您正在使用他们的标签来跟踪行.

Normally you would use the label's tag to know which of the two labels was tapped. But you are using their tags to track the row.

我推荐的解决方案是使用标签来区分两个标签.然后就可以根据标签的frame计算行了.

The solution I recommend is to use the tag to differentiate the two labels. Then you can calculate the row based on the frame of the label.

请参阅以下答案,了解将单元格子视图的框架转换为单元格的indexPath.

See the following answer for sample code that translates the frame of a cell's subview to the cell's indexPath.

这篇关于如何在 Swift 中知道发件人的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆