views:

219

answers:

3

Hi,

Currently i create 2 views and on the first view, there is a next button, upon clicking it, it will goto the second view.

Then in the second view, i set some value in the label and when i click back, it will go back to the first view.

Problem: now i am back in the first view, if i click the next button again, the label value i set in the second view is lost.

Question: How can i save the state such that when i click the next button, it will detect that the label is previously set and as such display back the previous value.

+1  A: 

It depends. There are many ways.

I expect that each time you push the second view to the screen, you are creating a new instance of it. In any case, whether you are or not, you should save the value of the label in the model rather than expecting the view to retain data (see: MVC Design Pattern). Then, in the -viewDidLoad: function of the second view controller, set the value of the label to its previous value.

Ed Marty
Hi thank for the responsive reply.Ya, i manage to do what you suggested which is set the value in viewDidLoad function.Also like to understand from you what are the other ways that you mention ? I was wondering, if we generally have a lots of data to say, is there a more efficient ways ?
+1  A: 

MVC is your friend.

Your view only displays data stored in model. So if you display person info the data are stored in person object. Your view would have a reference to person object that it is displaying so that it can read data. View does not set any data on person object, that is done via controller. Your controller bridges your model (data) and view by holding references to both.

User clicks on an item on display (e.g. modify user name), this is handled by controller who updates the model of person with new name. Once the action is completed controller asks view to redisplay data in model.

Have a look at Model-View-Controller and UIViewController in Apple's references.

stefanB
A: 

Hi thank for the responsive reply. Ya, i manage to do what you suggested which is set the value in viewDidLoad function. Also like to understand from you what are the other ways that you mention ? I was wondering, if we generally have a lots of data to say, is there a more efficient ways ?