0

我对开发 iOS 应用程序比较陌生。

我正在开发一个“note-app”,用户可以在其中在文本字段中输入信息,点击保存,然后有机会完全退出应用程序,重新打开,然后通过按钮加载信息。

我目前正在使用 Xcode 4.5。

我已经编写了所有代码并且发生了一些奇怪的事情。我在每个文本字段中输入一些内容(我有十二个),点击保存,然后退出应用程序。当我重新打开应用程序并点击加载时,文本字段 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;

第二视图控制器.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!” 意味着保存和加载。

4

2 回答 2

1

在 myTextField 上设置数据的每个地方都[myTextField setText:loadstring]; 应该是myTextfield1,2,3....等等。
  此外,您不必每次都初始化 NSUserDefaults 。NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];只需在函数顶部定义它。

于 2013-01-15T03:58:47.113 回答
0

当您加载每个字符串时,您会一遍又一遍地设置“myTextField”。那是第一个领域。

顺便说一句,没有必要为您要读取/写入的每个字段获取指向标准用户默认值的指针。

于 2013-01-15T04:00:34.487 回答