Save the state of the iphone sdk state

I am currently creating 2 views and the next button appears on the first view, after clicking it, it goes to the second view.

Then in the second view, I set some value in the label, and when I click the back button, it goes back to the first view.

Problem: I am now back to the first view, if I click the next button again, the label value set in the second view will be lost.

Question: How can I keep the state in such a way that when the next button is pressed, it finds that the label is already set and as such displays the previous value.

+1


a source to share


2 answers


It depends. There are many ways.



I expect that every time you click the second image on the screen, you create a new instance of it. In any case, whether you are or not, you should store the label value in the model, not expect the view to persist the data (see MVC Design Pattern). Then, in the function of the -viewDidLoad:

second view controller, set the label value to its previous value.

+1


a source


MVC is your friend.

Your view only displays the data stored in the model. Therefore, if you display information about a user, the data is stored in the person object. Your view will have a link to the human object it displays so that it can read the data. The view does not set any data about the human object that is executed through the controller. Your controller bridges your model (data) and views, keeping references to both.



The user clicks on the displayed item (for example, changes the username), this is handled by the controller, which updates the person model with the new name. Once the action is complete, the controller requests a view to re-display the data in the model.

Have a look at Model-View-Controller and UIViewController in Apple links.

+1


a source







All Articles