我正在嘗試一個簡單的練習,在應用程序關閉時將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];
}
是否有這個代碼,我缺少的東西之內?當我啓動應用程序時,它運行良好,沒有錯誤。我關閉了應用程序,仍然可以,但是當我通過多任務關閉應用程序並重新打開時,我收到了SIGKILL消息。
任何幫助將不勝感激。
你應該說明你的問題標題更詳細。 「我究竟做錯了什麼?」可以是任何類型的問題。 – Sebastian