Advertise here




Advertise here

Howdy, Stranger!

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

Simple way to pass data btw views?

rocotilosrocotilos Posts: 3,295iPhone Dev SDK Supporter, Registered Users @ @ @ @ @
edited June 2012 in iOS SDK Development
Yo what'sup

part of my app does this:

MainView -> UITableView --> DetailView.

I am currently implementing the protocol way to pass data btw views
as in the answer of this question: iphone - How to pass data back from detail view controller to uitableview? - Stack Overflow

However I was thinking, how about using NSUserDefaults to do this?

For example, when I selected something in the "detailview" can I just
save it to NSUserDefaults, and then upon returning to MainView,
I just load the NSUserDefaults back up to update whatever.

Would it work? Or perhaps the correct question, is there a case
where it will not work?

Thanks for any inputs.
Post edited by rocotilos on

Replies

  • Duncan CDuncan C Posts: 9,112Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited June 2012
    rocotilos wrote: »
    Yo what'sup

    part of my app does this:

    MainView -> UITableView --> DetailView.

    I am currently implementing the protocol way to pass data btw views
    as in the answer of this question: iphone - How to pass data back from detail view controller to uitableview? - Stack Overflow

    However I was thinking, how about using NSUserDefaults to do this?

    For example, when I selected something in the "detailview" can I just
    save it to NSUserDefaults, and then upon returning to MainView,
    I just load the NSUserDefaults back up to update whatever.

    Would it work? Or perhaps the correct question, is there a case
    where it will not work?

    Thanks for any inputs.


    Yes, user defaults would work. The real question is, is it the right way to do it?

    User defaults is disk-based. It's serious overkill for passing in-memory data. For purely in-memory transfer of data, a data container singleton is a much better choice.

    However, if you want the data you're saving to persist when the user presses the home button and then comes back to your app then user defaults IS a good choice. To do that, you want to save the currently active view controller and its state data as you are leaving the foreground.
    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
  • rocotilosrocotilos Posts: 3,295iPhone Dev SDK Supporter, Registered Users @ @ @ @ @
    edited June 2012
    Duncan C wrote: »
    Yes, user defaults would work. The real question is, is it the right way to do it?

    User defaults is disk-based. It's serious overkill for passing in-memory data. For purely in-memory transfer of data, a data container singleton is a much better choice.

    However, if you want the data you're saving to persist when the user presses the home button and then comes back to your app then user defaults IS a good choice. To do that, you want to save the currently active view controller and its state data as you are leaving the foreground.

    Thanks for your thoughts, Duncan.

    ;)

    On 2nd thought, I have another question:
    There are so many types of singleton... which do you use,
    and why.
  • rocotilosrocotilos Posts: 3,295iPhone Dev SDK Supporter, Registered Users @ @ @ @ @
    edited June 2012
    Anyway, I choose to use this type. Found a lot of variations in SO, I just pick one lol.
    + (MySingletonCenter *)sharedSingleton
    {
        @synchronized(shared)
        {
            if ( !shared || shared == NULL )
            {
                // allocate the shared instance, because it hasn't been done yet
                shared = [[MySingletonCenter alloc] init];
            }
            
            return shared;
        }
    }
    
  • MarkCMarkC Yorkshire, EnglandPosts: 1,967iPhone Dev SDK Supporter, Registered Users @ @ @ @
    edited June 2012
    Personally, I store data like that in the singleton (globals.m lol), and then at 'choice moments' save the whole lot off to User Defaults, loading them back in via User Defaults into the singleton when the app next runs. I would assume that's pretty common practice?
    neon_images_framed.jpg
  • rocotilosrocotilos Posts: 3,295iPhone Dev SDK Supporter, Registered Users @ @ @ @ @
    edited June 2012
    MarkC wrote: »
    Personally, I store data like that in the singleton (globals.m lol), and then at 'choice moments' save the whole lot off to User Defaults, loading them back in via User Defaults into the singleton when the app next runs. I would assume that's pretty common practice?

    I've been using global variables (those extern thingy) for a while for some of my apps. however some other are using protocol/delegate methods. singleton seems to be way easier though, i'm switching to that for all of my apps.
Sign In or Register to comment.