Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HRman93

macrumors newbie
Original poster
Sep 1, 2009
2
0
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
 
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

you could save and load it from a property list.
 
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

nsuserdefaults, if you need more elaboration just say so and id be happy to whip up some sample code tomorrow for you...
 
a sample would be very helpful. thank you

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.
 
Proper use of NSUserDefaults also includes using registerDefaults:

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.
 
@ 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.