PersistenceViewController.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:3k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  PersistenceAppDelegate.m
  3. //  Persistence
  4. //
  5. //  Created by Jeff LaMarche on 7/29/08.
  6. //  Copyright __MyCompanyName__ 2008. All rights reserved.
  7. //
  8. #import "PersistenceViewController.h"
  9. #import "FourLines.h"
  10. @implementation PersistenceViewController
  11. @synthesize field1;
  12. @synthesize field2;
  13. @synthesize field3;
  14. @synthesize field4;
  15. - (NSString *)dataFilePath
  16. {
  17. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  18. NSString *documentsDirectory = [paths objectAtIndex:0];
  19. return [documentsDirectory stringByAppendingPathComponent:kFilename];
  20. }
  21. - (void)applicationWillTerminate:(NSNotification *)notification
  22. {
  23. // NSMutableArray *array = [[NSMutableArray alloc] init];
  24. // [array addObject:field1.text];
  25. // [array addObject:field2.text];
  26. // [array addObject:field3.text];
  27. // [array addObject:field4.text];
  28. // [array writeToFile:[self dataFilePath] atomically:YES];
  29. // [array release];
  30. FourLines *fourLines = [[FourLines alloc] init];
  31. fourLines.field1 = field1.text;
  32. fourLines.field2 = field2.text;
  33. fourLines.field3 = field3.text;
  34. fourLines.field4 = field4.text;
  35. NSMutableData *data = [[NSMutableData alloc] init];
  36. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  37. [archiver encodeObject:fourLines forKey:kDataKey];
  38. [archiver finishEncoding];
  39. [data writeToFile:[self dataFilePath] atomically:YES];
  40. [fourLines release];
  41. [archiver release];
  42. [data release];
  43. }
  44. #pragma mark -
  45. - (void)viewDidLoad {
  46. NSString *filePath = [self dataFilePath];
  47. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
  48. {
  49. NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  50. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  51. FourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];
  52. [unarchiver finishDecoding];
  53. field1.text = fourLines.field1;
  54. field2.text = fourLines.field2;
  55. field3.text = fourLines.field3;
  56. field4.text = fourLines.field4;
  57. [unarchiver release];
  58. [data release];
  59. }
  60. UIApplication *app = [UIApplication sharedApplication];
  61. [[NSNotificationCenter defaultCenter] addObserver:self
  62.  selector:@selector(applicationWillTerminate:)
  63.  name:UIApplicationWillTerminateNotification 
  64.    object:app];
  65. [super viewDidLoad];
  66. }
  67. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  68. // Return YES for supported orientations
  69. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  70. }
  71. - (void)didReceiveMemoryWarning {
  72. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  73. // Release anything that's not essential, such as cached data
  74. }
  75. - (void)dealloc {
  76. [field1 release];
  77. [field2 release];
  78. [field3 release];
  79. [field4 release];
  80. [super dealloc];
  81. }
  82. @end