标签:
属性列表:
1 //插入4个文本框 2 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray * lineFields; 3 4 5 //获取路径,检查数据文件是否存在,存在则显示在文本框中。 6 - (void)viewDidLoad { 7 [super viewDidLoad]; 8 NSString * filePath = [self dataFilePath]; 9 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 10 NSArray * array = [[NSArray alloc] initWithContentsOfFile:filePath]; 11 for (int i = 0; i < 4; i++) { 12 UITextField * theField = self.lineFields[i]; 13 theField.text = array[i]; 14 } 15 } 16 17 //终止运行或者进入后台之前保存数据。 18 UIApplication * app = [UIApplication sharedApplication]; 19 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; 20 } 21 22 //获取文件路径,并返回。 23 - (NSString *)dataFilePath 24 { 25 NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 26 NSString * documentsDirectory = [paths objectAtIndex:0]; 27 return [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; 28 } 29 30 //终止运行或者进入后台之前执行的方法。 31 - (void)applicationWillResignActive:(NSNotification *)notification 32 { 33 NSString * filePath = [self dataFilePath]; 34 35 //通过调用lineFields数组中每个文本框的text方法构建一个字符串数组,对数组调用valueForKey:方法,并传递@“text”作为参数,实现了迭代获取实例变量的text值。 36 NSArray * array = [self.lineFields valueForKey:@"text"]; 37 [array writeToFile:filePath atomically:YES]; 38 }
标签:
原文地址:http://www.cnblogs.com/fengmin/p/4673753.html