-2

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?

thanks in advance

jbiser361
  • 949
  • 7
  • 20
mick1996
  • 516
  • 9
  • 31
  • You probably shouldn't. You should assign the value to a property of the `PopView` instance when you create it – Paulw11 Mar 21 '20 at 21:13
  • @Paulw11 how would I do that? – mick1996 Mar 21 '20 at 21:17
  • 1
    Declare something like `var textValue: String?` in `PopView` (Note that the class name should start with an uppercase P) and use a `didSet` handler on it to assign it to `titleLabel.text` – Paulw11 Mar 21 '20 at 21:20
  • Throwing this out here for future reference: Search your topic before you question it. for instance: https://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift that has the same exact question – jbiser361 Mar 22 '20 at 02:31
  • Does this answer your question? [Access variable in different class - Swift](https://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift) – jbiser361 Mar 22 '20 at 02:32

2 Answers2

3

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. You can then assign a value to this property when you create the PopView instance.

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)
        }
    }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

You simply cant access 'restaurantName' variable in the 'popView' class since the "popupView" class is an instance of "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.

pop.titleLabel.text = restaurantName

also change your "popView" class to the following

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
}
Mr Spring
  • 533
  • 8
  • 17