ElectricTiger
ElectricTiger

Reputation: 111

Change View Controllers while maintaining users input data

What is the best method to change between view controllers while keeping all text field data and segmented controller selections in the state the user chooses? Preferably in swift.

I have the need to change from 1 view controller to one of 3 others then always return to my main view controller before choosing the next controller to goto. I have found the using segues seems to reset my choices. Is there a way to navigate between controllers and leave the text fields populated? The actions that the user chooses impacts on some calculations on the main page. I want the user to be able to go back and forth adding and changing what they put in each page without re typing every box.

I would be grateful for any suggestions

Thank you

Upvotes: 0

Views: 618

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

You can save your data into NSUserDefaults and you can get that data anywhere, here is the simple example for you.

you can save data like this:

NSUserDefaults().setObject("your text from the text field", forKey: "YouKeyForThat")

after that you can get that data from any viewController like this way:

let loadedString = NSUserDefaults().stringForKey("YouKeyForThat") ?? ""

In your case you can load it and populate the textfield in viewDidLoad method With the info from disk.

myTextField.text = loadedString

Upvotes: 1

Related Questions