How do you make a textfield keep its text

Discussion in 'iOS Programming' started by HRman93, Sep 1, 2009.

  1. HRman93 macrumors newbie

    Joined:
    Sep 1, 2009
    #1
    I need to know how to make a textfield keep its text everytime the user opens the app. i have tried sending text to a label but cant get that to work so thought i'd just have the textfield keep the text. any ideas
     
  2. dickTater macrumors newbie

    Joined:
    Sep 1, 2009
    #2
    you could save and load it from a property list.
     
  3. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #4
    nsuserdefaults, if you need more elaboration just say so and id be happy to whip up some sample code tomorrow for you...
     
  4. HRman93 thread starter macrumors newbie

    Joined:
    Sep 1, 2009
  5. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #7
    alright, identify the textfield in the .h (and connect it appropiately in IB). this example will use "textField" as the name of the textfield. The "saving" action can be called by either a save button, when you hide the keyboard, or when the application is terminating (of course there are more, but those would likely be ideal). I'll just use an action called "yourAction."

    .m
    Code:
    -(IBAction)yourAction:(id)sender {
    //Call and save the settings (you can use anything, not just yourSaveSettings)
    	NSUserDefaults *yourSaveSettings = [NSUserDefaults standardUserDefaults];
    [yourSaveSettings setObject:[textField text] forKey: @"textFieldKey"];
    
    choose when you would like the save to be loaded (i used viewDidLoad)
    Code:
    //Identify when to load the settings
    -viewDidLoad {
    NSUserDefaults *yourSaveSettings = [NSUserDefaults standardUserDefaults];
    //Load 'em up
    textField.text = [yourSaveSettings stringForKey:@"textFieldKey"];
    }
    
    that should do it. NSUserDefaults are also great for bools, label text, values, etc.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Proper use of NSUserDefaults also includes using registerDefaults:
     
  7. peacetrain67 macrumors member

    Joined:
    Dec 20, 2007
    #9
    good point, if you havent set up any 'default' defaults up (before a setting has ever been saved), you could use registerDefaults or something like:
    Code:
    	if (![yourSettings objectForKey:@"textFieldKey"])
    		[yourSettings setObject:@"Default text field text" forKey: @"textFieldKey"];
    *i have only used this for BOOLs so i can't guarantee everything works there but I would assume that is how everything would be configured for a textfield.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    @ peacetrain67, Don't do it that way. Just use registerDefaults: For instance:

    Code:
    + (void)initialize
    {
       if(self == [MyClass class])
       {
    	[self registerDefaults];
       }
    }
    + (void)registerDefaults
    {
    	// Create factory settings and register them with the system
    	NSMutableDictionary*	defaultValues = [NSMutableDictionary dictionary];
    	
    	[defaultValues setObject:[NSNumber numberWithBool:YES] forKey:kMyKey];
    
    	[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
    }
    If you have some defaults that will be read by multiple classes then you probably want to call the class method, like I show above, for registerDefaults from your app delegate's +initialize method.
     

Share This Page