Tooti Tooti
Tooti Tooti

Reputation: 109

Swift: change @IBOutlet UIlabel text from another class

ViewController.swift

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var nowOnline: UILabel!

    override func viewDidLoad() {
        setText("NEW");
    }

    func setText(newText: String){
        nowOnline.text = "\(newText)"
    }
}

This code work fine, but if i want change it from another class not work

class SelectBox {
    func select(){
        let x = ViewController()
        x.setText("From Class") // this line i get error
    }
}

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

EDIT:

setText function :

internal func setText(){
    var priceLast = Int(self.selectPackPrice - AppDelegate.userCharge - self.selectExPrice)
    if(priceLast <= 0){
        priceLast = 0;
        txtGoPayment.text = "Call"
    } else {
        txtGoPayment.text = "Payment"
    }

    txtPackPrice.text = "Price : \(self.selectPackPrice) $"
    yourCharge.text = "Charge : \(AppDelegate.userCharge) $"
    priceFinish.text = "Finish Price : \(priceLast) $"
}

Upvotes: 0

Views: 242

Answers (1)

cvld
cvld

Reputation: 87

view.layoutSubviews() added to setText did it for me

Upvotes: 1

Related Questions