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