0

This should be easy but I am unable to figure it out, yet.

Situation

View controller 1 (text field: text_field_1)

View controller 2 (text field: text_field_2)

There is a "show segue" from view controller 1 to view controller 2, also navigation controller is included as well.

Problem

In veiw controller 1, user types something in "text_field_1",

then segue to view controller 2, user types something in "text_field_2",

then go back to view controller 1 by using navigation controller, the text typed in "text_field_1" is preserved

then segue to view controller 2 again, but the text typed in "text_field_2" is not preserved

What I want to achieve

Is there any way to preserve text in "text_field_2"?

Alvin
  • 515
  • 6
  • 11

3 Answers3

1

It's the normal behavior cause once you popped your view controller 2, it gets deallocated. There're several solutions to your issue:

  • Either create a delegate to notify VC1 when VC2 textfield text changed, store this value in VC1, and pass this value to the VC2 when the show segue is triggered again. In your VC2 viewDidLoad: method, check whether this value is nil ; if not, set your text_field_2 default text.
  • In your VC2, save the textfield text into the NSUserDefaults in the viewWillDisappear: (for example) method, and on load, check whether you can retrieve a value from the NSUserDefaults. If yes, set your text_field_2 text value.

Let me know if you need a source code example for either solution 1 or 2.

sweepy_
  • 1,333
  • 1
  • 9
  • 28
1

VC1 is your 'original' view controller. It then creates an instance of VC2 which is pushed onto the navigation stack and displayed. When you press the back button, it is popped off the navigation stack and destroyed. The original VC1 is still there in the background, intact. Transitioning to VC2 again causes a new instance of VC2 to be created, hence the empty text field.

If you want the value of the text field in VC2 to persist, you need to store it manually, either in a file of your own creation, NSUserDefaults, Core Data or whatever.

dandan78
  • 13,328
  • 13
  • 64
  • 78
0

Although all the given answers are correct, they do not explain the problem at hand:

Why do view controllers loose their values once you pop them?

View controllers play a specific role in the MVC design of Cocoa applications. As the official Apple documentation puts it:

View controllers are traditional controller objects in the Model-View-Controller (MVC) design pattern, [...]

As such, they are responsible for handing over values from the underlying model to their view and propagating changes back to the model layer:

A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects.

Communication: A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it.

In particular, view controllers are no model objects. They should not hold (other than transient) data.

Whenever you pop a view controller, it is getting deallocated because it is not supposed to hold persistent data.

Community
  • 1
  • 1
Dennis
  • 2,119
  • 20
  • 29
  • Thanks for the reply. Currently, I have a model object for holding all data. When switching between view controller, I pass the model object from one view controller to another one. Is there any way to make it as a global model object so I can simply refer to this model object in any view controllers without passing it around? (the data is unsuitable to be kept in db or user default), Thanks. – Alvin Mar 23 '15 at 13:50
  • Yes. You can create a singleton which holds the data. – Dennis Mar 23 '15 at 13:52
  • See, e.g., http://stackoverflow.com/q/5720029/1770453 for how to write the singleton's `sharedInstance` method, so that it's accessible by all view controllers. – Dennis Mar 23 '15 at 13:55