-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

5
  • You probably shouldn't. You should assign the value to a property of the PopView instance when you create it Commented Mar 21, 2020 at 21:13
  • @Paulw11 how would I do that? Commented Mar 21, 2020 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 Commented Mar 21, 2020 at 21:20
  • Throwing this out here for future reference: Search your topic before you question it. for instance: stackoverflow.com/questions/24333142/… that has the same exact question Commented Mar 22, 2020 at 2:31
  • Does this answer your question? Access variable in different class - Swift Commented Mar 22, 2020 at 2:32

2 Answers 2

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)
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Paul! is this still bad practice tho?
No, why would it be? PopView is uncoupled from the view controller; I.e. any view controller can use PopView and assign a value to the label.
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.