自动保存标签中的信息并在 iOS 中切换

Posted

技术标签:

【中文标题】自动保存标签中的信息并在 iOS 中切换【英文标题】:Autosave info from label and switch in iOS 【发布时间】:2013-01-14 21:38:24 【问题描述】:

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

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

我目前使用的是 Xcode 4.5。有没有聪明的xcoder可以给我提示?

所有帮助将不胜感激!

编辑

我使用了保存/加载功能,但发生了一些奇怪的事情。发生的情况是,当我在每个文本字段(我有十二个)中输入内容时,点击保存,然后退出应用程序。当我重新打开应用程序并点击从文本字段 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!"];


对于那些挪威人! “Dine notater ble lastet inn!”和“Dine notater ble lagret!”表示已保存并加载。

再次,如果有人有时间浏览这些内容,我将不胜感激!

【问题讨论】:

【参考方案1】:

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

这是一个非常简单的答案,但这就是它的要点,而无需为您编写整个功能。随着您获得更多知识,您可以以此为基础(例如,实现textField:shouldChangeCharactersInRange:replacementString: 并使用计时器来保存用户的进度,即使他们没有退出文本字段等)。

【讨论】:

好吧,我刚刚意识到我真的不擅长这个。什么都不懂。最好回到学校!

以上是关于自动保存标签中的信息并在 iOS 中切换的主要内容,如果未能解决你的问题,请参考以下文章

React 中的状态自动保存(KeepAlive)

如何实现 React 中的状态自动保存?

Python使用matplotlib保存图像时发生自动裁剪丢了部分标签信息解决方案(plt.savefig保存时丢失了部分标签字符)

IOS中的即时/自动保存

在 VS Code 中保存时关闭自动关闭的 HTML 标签?

是否可以使用 Live-server for PHP 并在保存时自动重新加载?