ios学习笔记-属性列表(做一个简单的记事本)

对于只需要保存简单的数据的应用,使用属性列表是一个不错的选择,nsarray,nsdictionary都提供了 array writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>和arrayWithContentsOfFiles的方法,一个是写入数据,一个是恢复数据用的。很是方便呢。。哈哈。。<span style="color: rgb(0, 175, 202); font-family: Menlo; font-size: 18px;"></span>
//
//  ViewController.m
//  属性链表
//
//  Created by lyx on 16/3/10.
//  Copyright © 2016年 . All rights reserved.
//

#import "ViewController.h"

#define SCREEFRAME [UIScreen mainScreen].bounds.size
@interface ViewController ()

@property(nonatomic,strong) NSMutableArray *labelArray;
@property(nonatomic,strong) NSMutableArray *filedArray;

@end

@implementation ViewController

int nextY = 80;
int i = 1;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.labelArray = [NSMutableArray array];
    self.filedArray = [NSMutableArray array];
    
    UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 20, SCREEFRAME.width, 44)];
    [self.view addSubview:navigationBar];
    UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"属性列表"];
    navigationBar.items = [NSArray arrayWithObject:item];
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]initWithTitle:@"增加" style:UIBarButtonItemStyleBordered target:self action:@selector(adItem:)];
    UIBarButtonItem *removeBtn = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(removeBtn:)];
    item.leftBarButtonItems = [NSArray arrayWithObjects:addBtn,removeBtn, nil];
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(saveBtn:)];
    item.rightBarButtonItem = saveBtn;
    
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:[self filePath]];
    for (NSString *field in contentArray) {
        [self addItem:nil content:field];
    }
    
    
}
-(void)adItem:(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.filedArray addObject:textfield];
    [self.view addSubview:textfield];
    nextY += 40;
    i++;
    
}
-(void)resign:(id)sender
{
    [sender resignFirstResponder];
}
-(void)removeBtn:(UIBarButtonItem *)item
{
    UILabel *lastLabel = [self.labelArray lastObject];
    UITextField *lastTextField = [self.filedArray lastObject];
    
    [lastLabel removeFromSuperview];
    [lastTextField removeFromSuperview];
    [self.labelArray removeObject:lastLabel];
    [self.filedArray removeObject:lastTextField];
    nextY -= 40;
    i--;
}
-(void)saveBtn:(UIBarButtonItem *)item
{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (UITextField *field in self.filedArray) {
        [array addObject:field.text];
    }
    [array writeToFile:[self filePath] atomically:YES];
    
   

                            
}
-(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPatn = [paths objectAtIndex:0 ];
    return [NSString stringWithFormat:@"%@/myList.plist",documentPatn];
}



@end

ios学习笔记-属性列表(做一个简单的记事本)_第1张图片

ios学习笔记-属性列表(做一个简单的记事本)_第2张图片

文件路径:

/Users/xxt/Library/Developer/CoreSimulator/Devices/7294DA9A-E19B-447F-A51B-C6640D607685/data/Containers/Data/Application/DC707480-30D2-4264-8563-538134A23D34/Documents

你可能感兴趣的:(ios,数据列表)