Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
shane reichefeld
Courses Plus Student 2,232 PointsNSUserDefault and segue
ok so i have everything set up for my NSUserDefaults and its set up in my viewdidLoad
if (txt1TotalDefault.valueForKey("TextField1Total") != nil){
TextField1Total = txt1TotalDefault.valueForKey("TextField1Total") as! Double
let T1Total = NSString(format: "TextField1Total: %i", TextField1Total)
}
i need to get T1Total out so i can put it into my segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController
DestViewController.TotalMoneyLabel.text = T1Total
}
any suggestions
1 Answer
ianhan3
4,263 PointsInstantiate the variable outside the viewDidLoad block as a class variable.
var T1Total = NSString()
override func viewDidLoad() {
super.viewDidLoad()
if (txt1TotalDefault.valueForKey("TextField1Total") != nil){
T1Total = NSString(format: "TextField1Total: %i", TextField1Total)
}
}
Is there a reason you are using NSString? If there isn't a specific reason, switch to the NSUserDefaults string methods. Use Camel Case when naming variables and make your variables more descriptive if you can. You'll thank yourself later in larger projects.
let defaults = NSUserDefaults.standardUserDefaults()
var variableToPass = String()
override func viewDidLoad() {
super.viewDidLoad()
if defaults.stringForKey("keyNameFromDefaults") != nil {
let valueFromDefaults = defaults.stringForKey("keyNameFromDefaults")!
variableToPass = "This is the value from the user defaults: \(valueFromDefaults)"
}
}
shane reichefeld
Courses Plus Student 2,232 Pointsshane reichefeld
Courses Plus Student 2,232 Pointsi dont understand how to fix my problem still, there is no reason for the NSString and i cant get my code to work so that my label in another view changes to T1Total
shane reichefeld
Courses Plus Student 2,232 Pointsshane reichefeld
Courses Plus Student 2,232 Pointsi dont understand how to fix my problem still, there is no reason for the NSString and i cant get my code to work so that my label in another view changes to T1Total