Save Text Field Using the NSUserDefaults Class

Discussion in 'iOS Development' started by Rockstarou812, Feb 22, 2009.

  1. Rockstarou812

    Rockstarou812 New Member

    Joined:
    Jan 17, 2009
    Messages:
    43
    Likes Received:
    0
    My app needs to be able to "save" information. It has four text fields for peoples names. While the app is running in the simulator you can add them in and they stay, but I need them to be put in, and then stay there every-time the app is opened!

    I read this doc, but it just confused me.

    Will this involve a lot of code? Hopefully its easy, because this app is very simple. Just one xib, and one .m and one .h

    Please Register or Log in to view images

  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [userDefaults setObject:yourString forKey

    Please Register or Log in to view images

    "SomeKey"];
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Then to retrieve:
    Code:
    	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    	NSString * string = [prefs floatForKey:@"SomeKey"];
    	label.text = [[NSString alloc] initWithFormat:@"%@", string];
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Wrong. Look at this line:
    Code:
    NSString * string = [prefs floatForKey:@"SomeKey"];
    //This is wrong

    It should be NSString *aString = [userDefaults objectforKey

    Please Register or Log in to view images

    "SomeKey"];
    If you want to set it immediately to a label, label.text = [userDefaults objectForKey

    Please Register or Log in to view images

    "SomeKey"];
  6. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Yes your right.
    I was trying to switch it quickly from a float value to a string.
  7. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    Summarized in "one-liners":

    Code:
    // Save Content
    [[NSUserDefaults standardUserDefaults] setObject: label.text forKey: @"SomeRandomText"];
    
    // Load/Retrieve Content
    // Put this in the app delegate's applicationDidFinishLaunching:, view controller's viewDidLoad, or any other object's awakeFromNib method
    
    label.text = [[NSUserDefaults standardUserDefaults] objectForKey: @"SomeRandomText"];
    // If you really really need a copy, use this
    label.text = [NSString stringWithFormat: @"%@", [[NSUserDefaults standardUserDefaults] objectForKey: @"SomeRandomText"]];
    // Do NOT make new strings with the alloc/init methods; they will cause a memory leak because the label retains or copies its text
  8. Rockstarou812

    Rockstarou812 New Member

    Joined:
    Jan 17, 2009
    Messages:
    43
    Likes Received:
    0
    Thanks guys all this information looks great, I love this site! I really appreciate all you developers helping me out.

    I will try this later this week, I have been slammed with work recently.

    Thanks, I will tell u guys how it goes

    Please Register or Log in to view images



    Peace
  9. Rockstarou812

    Rockstarou812 New Member

    Joined:
    Jan 17, 2009
    Messages:
    43
    Likes Received:
    0
    Great I got it to work.

    Now what I want is to get the NSUserDefualts to work in a flipview app

    I want to have the textfields on the backside, and the text to display on the front, make sense?

Share This Page