Save data of a Label in swift -
I have a TextField in my settingsController that I can modify a selection from a pickerView, the problem is this: When I change the text of my textfield I also change the text of a label in another controller, it works, but when I close my app and open the label again, there is no way to save text. Which I can find it pickerView
My code settingsController
viewDidLoad function overrides () {super.viewDidLoad () var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults () to that firstNameIsNotNill = defaults. ObjectForKey ("FirstName") as? String {self.currencyLabel.text = defaults.objectForKey ( "as firstName") string} currencyLabel.delegate = self} currencyDoneClicked function (this: UIBarButtonItem) {var myRow = picker.selectedRowInComponent (0) currencyLabel.text = pickerData. objectAtIndex (myRow) NSString DataManager.sharedInstance.contenitore = currencyLabel.text God as defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults () defaults.setObject (self.currencyLabel.text, forKey: "firstName") defaults.synchronize ()} < / Code>
DataManager:
Import UIKit class DataManager: NSObject {sharedInstance on class: DataManager {{Mill struct Static {static example: DataManager? = Zero constant token: dispatch_once_t = 0} dispatch_once (& static .ken) {Static.instance = DataManager ()} Return Static.instance! }} Var contenitore: string! }
And another controller:
viewDidLoad () {super.viewDidLoad () labelCurrency.text = DataManager.sharedInstance.contenitore} function override
simply NSUserDefaults
.
NSUserDefaults.standardUserDefaults () setValue (textField.text, forKey: "savedTextField").
field Then, when you resume, populate viewDidLoad
or viewWillAppear
Go if text = NSUserDefaults.standardUserDefaults (). StringForKey ("savedTextField") {textField.text = text}
Comments
Post a Comment