属性列表
对于只需要保存简单数据的应用而言,使用属性列表是一个不错的选择,NSArray、NSDictionary对象都提供了writeToFile:(NSString *)filePath atomically:(BOOL)flag方法,该方法可以吧NSArray和NSDictionary包含的数据写入属性列表。
当恢复这些数据时,只要调用NSArray,NSDictionary的xxxWithContentsOfFile:(NSString*)filePath方法执行初始化即可。
下面以一个例子展示属性列表的使用规范:
#import ”XXXViewController.h"
@interface XXXViewController()
定义一个NSMutableArray记录所有动态添加的UILabel控件
@property (nontomic,strong)NSMutableArray* labelArray;
定义一个NSMutableArray记录所有动态添加的UITextField控件
@property (nontomic,strong)NSMutableArray*fieldArray;
@end
@implementation XXXViewController
定义一个变量来记录下一个将要添加的UILable的Y坐标
int nextY = 80;
定义一个变量,记录当前正在添加第几项
int i=1;
- (void)viewLoad
{
[super viewDidLoad];
初始化labelArray和fieldArray
self.labelArray = [NSMutableArray array];
self.fieldArray = [NSMutableArray array];
创建一个导航条,并将它添加到程序界面上。
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,20,320,44)];
[self.view addSubView:bar];
创建一个UINavigationItem导航项,并添加到導航條上。
UINavigationItem *item = [[UINavigation alloc] initWithTitle:@"属性列表“];
bar.items = [NSArray arrayWithObject:item];
创建导航条上的添加按钮
UIBarButtonItem *addBn = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleBordered target:self action:@select(addItem:)];
创建导航条上的删除按钮
UIBarButtonItem *removeBn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@select(removeItem:)];
将添加按钮,删除按钮添加到导航条的左边
item.leftBarButtonItems = [NSArray arrayWithObjects:addBn , removeBn , nil];
创建导航条上的保存按钮
UIBarButtonItem* saveBn = [[UIBarButtonItem alloc]
initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(save:)];
将保存按钮放在右边
item.rightBarButtonItem = saveBn;
使用NSArray加载属性文件
NSArray *contentArray = [NSArray arrayWithContentOfFile:[self filePath]];
遍历属性文件中包含的数据项,并调用addItem:content:方法将数据项显示出来
for (NSString *content in contentArray)
{
[self addItem:nil content:content];
}
}
- (void)addItem:(id)sender
{
[self addItem:sender content:nil];
}
- (void)addItem:(id)sender content:(NSString*)content{
UILabel* label = [[UILabel alloc] initWithFrame:
CGRectMake(10 , nextY , 80 , 30)];
label.text = [NSString stringWithFormat:@"第%d项" , i];
[self.labelArray addObject: label];
[self.view addSubview:label];
UITextField* textField = [[UITextField alloc] initWithFrame:
CGRectMake(100 , nextY , 210 , 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
if (content != nil && content.length > 0)
{
textField.text = content;
}
[textField addTarget:self action:@selector(resign:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.fieldArray addObject: textField];
[self.view addSubview:textField];
nextY += 40;
i++;
}
- (void)removeItem:(id)sender
{
UILabel* lastLabel = [self.labelArray lastObject];
UITextField* lastField = [self.fieldArray lastObject];
[lastLabel removeFromSuperview];
[lastField removeFromSuperview];
[self.labelArray removeObject:lastLabel];
[self.fieldArray removeObject:lastField];
nextY -= 40;
i--;
}
- (void) save:(id)sender
{
NSMutableArray* array = [[NSMutableArray alloc] init];
for (UITextField* tf in self.fieldArray)
{
[array addObject:tf.text];
}
[array writeToFile:[self filePath] atomically:YES];
UIActionSheet * sheet = [[UIActionSheet alloc]
initWithTitle:@"保存成功" delegate:nil cancelButtonTitle:nil
destructiveButtonTitle:@"确定" otherButtonTitles:nil];
[sheet showInView:self.view];
}
- (void) resign:(id)sender
{
[sender resignFirstResponder];
}
- (NSString*) filePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [NSString stringWithFormat:@"%@/myList.plist"
, documentsDirectory];
}
@end