我在viewController中设置了一个NSUserDefault,这是viewDidLoad中的一部分
if (txt1TotalDefault.valueForKey("TextField1Total") != nil){
TextField1Total = txt1TotalDefault.valueForKey("TextField1Total") as! Double
let T1Total = NSString(format: "TextField1Total: %i", TextField1Total)
}
然后我在所有代码的底部都有一个segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController
DestViewController.TotalMoneyLabel.text = T1Total}
但它无法正常工作,因为它无法识别T1Total,如何识别它或将其从if语句中删除,以便正确读取值
答案 0 :(得分:0)
T1Total
应该是类变量。在您的情况下,T1Total
方法无法访问其prepareForSegue
方法变量。
Class yourclass
{
var T1Total = ""
override func viewDidLoad()
{
if (txt1TotalDefault.valueForKey("TextField1Total") != nil){
TextField1Total = txt1TotalDefault.valueForKey("TextField1Total") as! Double
T1Total = NSString(format: "TextField1Total: %i", TextField1Total)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController
DestViewController.TotalMoneyLabel.text = T1Total
}
}
使用此代码,T1Total
prepareForSegue