Shane Reichenfeld Shane Reichenfeld - 5 months ago 16
iOS Question

PrepareForSegue Swift

I have a NSUserDefault all set up in my viewController and this is part of it in the viewdidLoad

if (txt1TotalDefault.valueForKey("TextField1Total") != nil){
TextField1Total = txt1TotalDefault.valueForKey("TextField1Total") as! Double
let T1Total = NSString(format: "TextField1Total: %i", TextField1Total)
}


i then have a segue at the bottom of all the code

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController

DestViewController.TotalMoneyLabel.text = T1Total}


but it will not work because it does not recognise T1Total, how would i get it to recognize it or get it out of the if statement so it can read the vaule correctly

Answer

T1Total should be class variable. In your case its method variable, because of this T1Total is not accessible in prepareForSegue method.

Pseudo Code

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
    }
}

Using this code the T1Total will be accessible in prepareForSegue

Comments