简体   繁体   English

访问来自不同类的变量

[英]Access a variable from a different class

I am trying to access a variable from a different class.我正在尝试访问来自不同类的变量。 What am I doing wrong?我究竟做错了什么?

class ViewController: UIViewController {

    var restaurantName = "Test"

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnClicked(_ sender: Any) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()){
            let pop = popView()
            self.view.addSubview(pop)
        }
    }
}

here is the class I am trying to access it from:这是我试图从中访问它的类:

class popView: UIView{

    fileprivate let titleLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.systemFont(ofSize:28, weight: .bold)
        label.textAlignment = .center
        //label.text = "TITLE"

        label.text = restaurantName
        return label
    }()
}

How can I access the 'restaurantName' variable in the 'popView' class?如何访问“popView”类中的“restaurantName”变量?

thanks in advance提前致谢

You don't want to tightly couple the view and the view controller.您不想将视图和视图控制器紧密耦合。

You should have a property on your PopView to hold the text.您应该在PopView上有一个属性来保存文本。 You can then assign a value to this property when you create the PopView instance.然后,您可以在创建PopView实例时为该属性分配一个值。

class PopView: UIView{

    fileprivate let titleLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.systemFont(ofSize:28, weight: .bold)
        label.textAlignment = .center
        //label.text = "TITLE"

        label.text = restaurantName
        return label
    }()

    var titleText: String? {
        didSet {
            self.titleLabel.text = titleText
        }
    }
}

class ViewController: UIViewController {

    var restaurantName = "Test"

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnClicked(_ sender: Any) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()){
            let pop = popView()
            pop.titleText = restaurantName
            self.view.addSubview(pop)
        }
    }
}

You simply cant access 'restaurantName' variable in the 'popView' class since the "popupView" class is an instance of "ViewController".您根本无法访问“popView”类中的“restaurantName”变量,因为“popupView”类是“ViewController”的一个实例。

If you want to assign property "restaurantName" to "titleLabel" simply remove the "fileprivate" from property "titleLabel" and add this line before the "addSubview" func.如果您想将属性“restaurantName”分配给“titleLabel”,只需从属性“titleLabel”中删除“fileprivate”并在“addSubview”函数之前添加这一行。

pop.titleLabel.text = restaurantName

also change your "popView" class to the following还将您的“popView”类更改为以下内容

class popView: UIView{

weak var titleLabel: UILabel!

func awakeFromNib() {
    super.awakeFromNib()
    titleLabel = UILabel()
    titleLabel.translatesAutoresizingMaskIntoConstraints = false
    titleLabel.font = UIFont.systemFont(ofSize:28, weight: .bold)
    titleLabel.textAlignment = .center
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM