Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Retaining Data when using Storyboards

thoford75thoford75 Posts: 8New Users
edited November 2011 in iPhone SDK Development
I have 8 detail view controllers and 1 main controller in a storyboard setup.

The main controller features a table with 8 rows which link (push) to each relevant module controller.

If I make a change on a module controller page then use the navigation (back) to return to the main controller and then try to go back into that page via the table links, the data resets.

How can I setup the storyboard so that data saves in-between navigation?

Cheers!
Post edited by thoford75 on

Replies

  • RickSDKRickSDK Posts: 1,016Registered Users @ @ @ @
    edited November 2011
    save data using userDefaults. Then read the data back in on viewdidload.
    <a href="http://www.pokertrackpro.com/">Poker Track Pro</a>
  • thoford75thoford75 Posts: 8New Users
    edited November 2011
    RickSDK wrote: »
    save data using userDefaults. Then read the data back in on viewdidload.

    How can I visually acheive this using storyboards?
  • RickSDKRickSDK Posts: 1,016Registered Users @ @ @ @
    edited November 2011
    its hard to tell exactly what you are trying to do, but you mentioned that the data "resets".

    If order to not get data to "reset", you need to read that data in from userDefaults rather than reading it in from the initial view controller data.

    For example if you go to a view controller and it defaults to a "red house" and then the user can change it to a "blue house" withint the controller, you need to save that blue data and when you reload the view, find out what color it is supposed to be by reading in the data from userDefaults, rather than defaulting to "red"

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"Blue" forKey:@"House"];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *houseColor = [userDefaults stringForKey:@"House"];
    <a href="http://www.pokertrackpro.com/">Poker Track Pro</a>
  • Duncan CDuncan C Posts: 9,021Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited November 2011
    RickSDK wrote: »
    its hard to tell exactly what you are trying to do, but you mentioned that the data "resets".

    If order to not get data to "reset", you need to read that data in from userDefaults rather than reading it in from the initial view controller data.

    Bollocks.

    You could use user defaults to save the state, but you don't have to do it that way.

    In fact, using user defaults is an ill-fitting approach to the problem of saving state in memory. User defaults writes files to disk, which is much slower, and causes the state save to persist even if you exit the app. (yes, it's much slower even in iOS, where the "disk" is really flash memory.)

    If you want the state to persist after exiting and re-launching then saving it to user defaults is a reasonable approach.

    If you're only saving state information for the case where a user switches from one view to another and back, it would be much better to use a data container singleton to save the state data there. (See the link to the password generator in my signature for an example of a data container singleton.)
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • thoford75thoford75 Posts: 8New Users
    edited November 2011
    Ideally what I am trying to build is a multipage form.

    1st Page - List of 8 Rooms - linking to 8 individual view controllers

    each 8 view controllers contains text fields that need to be saved. For example if the user selects from the 1st page 'Bedroom', fills in some text fields, then returns to the home page, selects 'Bathroom', fills in some text fields, the text fields in 'Bedroom' should remain either until a) the user clicks a button to email the information for all 8 rooms or b) closes the app.
  • thoford75thoford75 Posts: 8New Users
    edited November 2011
    Just a thought...but could it be todo with the use of the segue?

    I know if I pushed navigation from 1 to 2 to 3 to 4 and so on, the data would be save if I clicked back from 4 to 3.

    However sometimes I may want to go from 4 back to 1 (main screen) then back to 4 again and the data will reset (text box's to zero)

    ?
  • Duncan CDuncan C Posts: 9,021Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited November 2011
    thoford75 wrote: »
    Just a thought...but could it be todo with the use of the segue?

    I know if I pushed navigation from 1 to 2 to 3 to 4 and so on, the data would be save if I clicked back from 4 to 3.

    However sometimes I may want to go from 4 back to 1 (main screen) then back to 4 again and the data will reset (text box's to zero)

    ?

    It has to do with the way Storyboard works. I'm still trying to figure this out. It seems that storyboard always releases a view controller when you leave it, and always creates and initializes a new copy of the view controller you are switching to.

    I want to understand how you set up your program so storyboard switches between persistent instances of your view controllers. It does not seem like it 's set up to do that.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • thoford75thoford75 Posts: 8New Users
    edited November 2011
    If it helps here is a screen shot of the storyboard. The segues are all connected as Push
    sboardview.png
Sign In or Register to comment.