Swift:如何将字符串从UIView传递到数据源?

时间:2018-06-19 20:09:37

标签: ios swift delegates protocols datasource

我正在尝试将字符串值从一个类传递到另一个类。第一类是UIView,第二类是数据源。我试过使用委派,但似乎不起作用。关于如何做到这一点的任何想法?下面的代码。

发件人类别:

import LBTAComponents

protocol MatchesCellDelegate: class {
    var name: String? { get set }
}

class MatchesCell: DatasourceCell {

    weak var delegate: MatchesCellDelegate?

    let usernameLabel: UILabel = {
        let label = UILabel()
        label.text = "Username"
        return label
    }

    let prof: UIImageView = {
        let iv = UIImageView()
        iv.image = image
        return iv
    }

    @IBAction @objc func onTap(_ sender: UITapGestureRecognizer) {
        self.delegate?.name = usernameLabel.text!
    }

    override func setupViews() {
        super.setupViews()
        addSubview(usernameLabel)
        let tap = UITapGestureRecognizer(target: self, action: #selector(MatchesCell.onTap(_:)))
        prof.addGestureRecognizer(tap)
        prof.isUserInteractionEnabled = true
    }

}

接收器类:

import LBTAComponents

class ProfileDatasource: Datasource, MatchesCellDelegate {
    var name: String?
    var user = Profile(username: "", rank: 56)
    override func item(_ indexPath: IndexPath) -> Any? {
        return user
    }
}

如何在发送者类中将usernameLabel.text设置为在接收者类中的user.username?谢谢。

0 个答案:

没有答案