Swift:从另一个类更改@IBOutlet UIlabel文本

时间:2018-09-08 06:32:38

标签: swift class uiviewcontroller uilabel

  

ViewController.swift

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

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

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

此代码可以正常工作,但是如果我想从另一个类更改它,则无法正常工作

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

线程1:致命错误:展开包装时意外发现nil   可选值

编辑:

setText函数:

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) $"
}

0 个答案:

没有答案