iOS5를 문제의 데이터 : 아이폰 OS와저장 나는 시작 아이폰 개발에서이 코드를 사용하여 내 응용 프로그램에서 간단한 텍스트 데이터를 저장하기 위해 노력하고있어
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingFormat:kFileName];
}
- (void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad { NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:4];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
}
이 코드 작성 및 iOS 3 위대한 작품을했지만,하지 5. 시뮬레이터에서 홈 버튼을 눌러 간단히 닫은 다음 멀티 태스킹 막대를 위로 가져다가 앱 아이콘을 탭하면 데이터가 정상적으로 다시로드됩니다. 그러나 홈 버튼을 탭하여 닫은 다음 멀티 태스킹 막대를 가져 와서 닫고 앱을 다시 시작하면 데이터가 사라집니다. 나는 멀티 태스킹 막대에서 응용 프로그램을 닫을 때 응용 프로그램이 종료된다는 알림을 받고 있다고 생각하지 않지만 아직 초보자인지는 확실하지 않습니다.
나는 이미 가지고있는 코드에 여분의 줄 또는 2를 추가해야한다고 확신합니다. 누구든지 그게 뭔지 알아?
감사합니다. applicationWillTerminate라고 말한 모든 것을 applicationDidEnterBackground로 변경했습니다. 그러나 viewDidLoad의 끝 부분에서 "name :"아래에 UIApplicationWillTerminateNotification이 표시됩니다. UIApplicationWillEnterBackgroundNotification으로 변경하려고 시도했지만 오류가 발생했습니다. 그 대신에 내가 넣어야 할게 있니? – John
UIApplicationDidEnterBackgroundNotification –
정말 고마워요! 그것은 대단히 도움이되었습니다. – John