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
a sample would be very helpful. thank you
-(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"];
//Identify when to load the settings
-viewDidLoad {
NSUserDefaults *yourSaveSettings = [NSUserDefaults standardUserDefaults];
//Load 'em up
textField.text = [yourSaveSettings stringForKey:@"textFieldKey"];
}
Proper use of NSUserDefaults also includes using registerDefaults:
if (![yourSettings objectForKey:@"textFieldKey"])
[yourSettings setObject:@"Default text field text" forKey: @"textFieldKey"];
+ (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];
}