从iOS中的标签和开关自动保存信息

时间:2013-01-14 21:38:25

标签: ios objective-c cocoa-touch save load

我对开发iDevice应用程序比较陌生。在互联网上进行了广泛的搜索之后,我最终得到了一个没有答案的问题。

我正在开发一个“笔记应用”,用户可以在文本字段中输入信息,打开开关,然后有机会完全退出应用,重新打开,信息仍在那里。没有“保存”按钮。这可能吗?并且可能有一个“重置”按钮来丢弃所有信息。

我目前正在使用Xcode 4.5。有没有聪明的xcoders可以给我提示吗?

非常感谢所有帮助!

修改

我选择了save / load-functions,发生了一些奇怪的事情。当我在每个文本字段中输入内容(我有12个)时,点击保存并退出应用程序会发生什么。当我重新打开应用程序并点击加载文本字段中的文本12加载文本字段1.并且所有其他文本都消失了。任何人都有线索?

以下是有时间的人的代码。

SecondViewController.h

@interface SecondViewController : UIViewController <UITextFieldDelegate> {

    IBOutlet UITextField *myTextField;
    IBOutlet UITextField *myTextField2;
    IBOutlet UITextField *myTextField3;
    IBOutlet UITextField *myTextField4;
    IBOutlet UITextField *myTextField5;
    IBOutlet UITextField *myTextField6;
    IBOutlet UITextField *myTextField7;
    IBOutlet UITextField *myTextField8;
    IBOutlet UITextField *myTextField9;
    IBOutlet UITextField *myTextField10;
    IBOutlet UITextField *myTextField11;
    IBOutlet UITextField *myTextField12;

    IBOutlet UILabel *loaded; }

-(IBAction)save:(id)sender;
-(IBAction)load:(id)sender;

SecondViewController.m

@implementation SecondViewController

-(IBAction)save:(id)sender{

    //First Save String
    NSString *savestring =myTextField.text;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:savestring forKey:@"saveString"];
    [defaults synchronize];

    //Second Save String
    NSString *savestring2 =myTextField2.text;
    NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
    [defaults2 setObject:savestring2 forKey:@"saveString2"];
    [defaults2 synchronize];

    //Third Save String
    NSString *savestring3 =myTextField3.text;
    NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
    [defaults3 setObject:savestring3 forKey:@"saveString3"];
    [defaults3 synchronize];

    //Fourth Save String
    NSString *savestring4 =myTextField4.text;
    NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
    [defaults4 setObject:savestring4 forKey:@"saveString4"];
    [defaults4 synchronize];

    //Fifth Save String
    NSString *savestring5 =myTextField5.text;
    NSUserDefaults *defaults5 = [NSUserDefaults standardUserDefaults];
    [defaults5 setObject:savestring5 forKey:@"saveString5"];
    [defaults5 synchronize];

    //Sixth Save String
    NSString *savestring6 =myTextField6.text;
    NSUserDefaults *defaults6 = [NSUserDefaults standardUserDefaults];
    [defaults6 setObject:savestring6 forKey:@"saveString6"];
    [defaults6 synchronize];

    //Seventh Save String
    NSString *savestring7 =myTextField7.text;
    NSUserDefaults *defaults7 = [NSUserDefaults standardUserDefaults];
    [defaults7 setObject:savestring7 forKey:@"saveString7"];
    [defaults7 synchronize];

    //Eight Save String
    NSString *savestring8 =myTextField8.text;
    NSUserDefaults *defaults8 = [NSUserDefaults standardUserDefaults];
    [defaults8 setObject:savestring8 forKey:@"saveString8"];
    [defaults8 synchronize];

    //Ninth Save String
    NSString *savestring9 =myTextField9.text;
    NSUserDefaults *defaults9 = [NSUserDefaults standardUserDefaults];
    [defaults9 setObject:savestring9 forKey:@"saveString9"];
    [defaults9 synchronize];

    //Tenth Save String
    NSString *savestring10 =myTextField10.text;
    NSUserDefaults *defaults10 = [NSUserDefaults standardUserDefaults];
    [defaults10 setObject:savestring10 forKey:@"saveString10"];
    [defaults10 synchronize];

    //Eleventh Save String
    NSString *savestring11 =myTextField11.text;
    NSUserDefaults *defaults11 = [NSUserDefaults standardUserDefaults];
    [defaults11 setObject:savestring11 forKey:@"saveString11"];
    [defaults11 synchronize];

    //Twelvth Save String
    NSString *savestring12 =myTextField12.text;
    NSUserDefaults *defaults12 = [NSUserDefaults standardUserDefaults];
    [defaults12 setObject:savestring12 forKey:@"saveString12"];
    [defaults12 synchronize];

    loaded.text = @"Dine notater ble lagret!"; }
-(IBAction)load:(id)sender;{

    //First Load String
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadstring = [defaults objectForKey:@"saveString"];
    [myTextField setText:loadstring];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Second Load String
    NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring2 = [defaults2 objectForKey:@"saveString2"];
    [myTextField setText:loadstring2];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Third Load String
    NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring3 = [defaults3 objectForKey:@"saveString3"];
    [myTextField setText:loadstring3];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Forth Load String
    NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring4 = [defaults4 objectForKey:@"saveString4"];
    [myTextField setText:loadstring4];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Fifth Load String
    NSUserDefaults *defaults5 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring5 = [defaults5 objectForKey:@"saveString5"];
    [myTextField setText:loadstring5];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Sixth Load String
    NSUserDefaults *defaults6 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring6 = [defaults6 objectForKey:@"saveString6"];
    [myTextField setText:loadstring6];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Seventh Load String
    NSUserDefaults *defaults7 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring7 = [defaults7 objectForKey:@"saveString7"];
    [myTextField setText:loadstring7];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Eight Load String
    NSUserDefaults *defaults8 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring8 = [defaults8 objectForKey:@"saveString8"];
    [myTextField setText:loadstring8];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Ninth Load String
    NSUserDefaults *defaults9 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring9 = [defaults9 objectForKey:@"saveString9"];
    [myTextField setText:loadstring9];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Tenth Load String
    NSUserDefaults *defaults10 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring10 = [defaults10 objectForKey:@"saveString10"];
    [myTextField setText:loadstring10];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Eleventh Load String
    NSUserDefaults *defaults11 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring11 = [defaults11 objectForKey:@"saveString11"];
    [myTextField setText:loadstring11];
    [loaded setText:@"Dine notater ble lastet inn!"];

    //Twelvth Load String
    NSUserDefaults *defaults12 = [NSUserDefaults standardUserDefaults];
    NSString *loadstring12 = [defaults12 objectForKey:@"saveString12"];
    [myTextField setText:loadstring12];
    [loaded setText:@"Dine notater ble lastet inn!"];

}

对于那些挪威语的人! “晚餐吃晚饭!”和“吃晚餐!”意味着保存和加载。

同样,如果有人有时间浏览这些,我将非常感激!

1 个答案:

答案 0 :(得分:1)

您需要一个实现UITextFieldDelegate协议的类,并将其设置为文本字段的委托属性的值。在委托中,您希望实现textFieldDidEndEditing:方法,并在该方法中,将文本字段的值写入磁盘。 “磁盘”是指一些持久存储,例如.plist文件或SQLite数据库,甚至只是一个文本文件。在您的app delegate的-applicationDidFinishLaunching:withOptions:方法中,您要检查此文件是否存在,如果存在,请阅读它以确定如何预填充文本字段。

这是一个相当简单的答案,但这是它的要点,而不是为您编写这整个功能。您可以在获得更多知识的基础上进行构建(例如,实现textField:shouldChangeCharactersInRange:replacementString:并使用计时器来保存用户的进度,即使他们不退出文本字段等)。