2011-09-11 6 views
0

私は、アプリケーションが終了したときにUITextFieldの内容をファイルに保存する簡単な方法を試しています。アプリケーションが再起動したときにUITextFieldを再ロードする際の問題

次のように私のヘッダファイルがある:

#define kFileName @"file.plist" 

@interface applicationLaunchViewController : UIViewController { 

    UITextField *text1; 

} 

@property (nonatomic, retain) IBOutlet UITextField *text1; 

-(void)applicationSaveFile:(NSNotification *)notification; 
-(NSString *)dataFilePath; 

私の実装ファイル:

@implementation applicationLaunchViewController 

@synthesize text1; 

-(void)applicationSaveFile:(NSNotification *)notification { 

    NSMutableArray *array = [[NSMutableArray alloc] init]; 

    [array addObject:text1.text]; 
    [array writeToFile:[self dataFilePath] atomically:YES]; 
    [array release]; 

} 

-(NSString *)dataFilePath { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDictionary = [paths objectAtIndex:0]; 
    return [documentsDictionary stringByAppendingPathComponent:kFileName]; 

} 



- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 

    NSString *filePath = [self dataFilePath]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
     NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
     self.text1.text = [array objectAtIndex:0]; 
     [array release]; 
    } 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationSaveFile:) name:UIApplicationDidEnterBackgroundNotification object:nil]; 


    [super viewDidLoad]; 
} 

は私が欠けている、このコード内の何かがありますか?私はアプリを起動するとき、それは正常に動作し、エラーは発生しません。私はまだOKアプリを閉じますが、私はマルチタスクでアプリケーションを閉じて、私はSIGKILLメッセージを取得する再オープンに行く。

ご協力いただければ幸いです。

+1

あなたの質問のタイトルをさらに詳しく記述してください。 "私は間違って何をしていますか?"どんな種類の質問でもあります。 – Sebastian

答えて

0

マルチタスクでアプリケーションを終了すると、ほとんどの場合、簡単にキャッチできない(おそらく決してない)Sigkillが得られます。

UITextFieldのテキストを保存していますが、これはユーザーが前提としています。データはapplicationDidEnterBackgroundに保存する必要があります。あなたのアプリがバックグラウンドにある場合、ユーザはsthを書くことができません。あなたのテキストフィールドに。

0

おそらく、アプリが終了したとき(アプリをマルチタスクウィンドウから終了する)に通知を受け取る必要があります。

アプリデリゲートには、アプリを終了する直前に呼び出されるメソッドがあり、終了する前にファイルに書き込んだり、通知を終了または登録解除する前にファイルに書き込む時間を追加することができます。

- (void)applicationWillTerminate:(UIApplication *)application { 
    /* 
    Called when the application is about to terminate. 
    See also applicationDidEnterBackground:. 
    */ 
} 
+0

お返事ありがとうございます。あなたは助けてくれました – ShedInTheGarden

関連する問題