FirstApp,iphone开发学习总结10,归档(序列化、固化)

归档TableViewController的data。

为了在Nav上添加Add按钮,为TableViewController创建UINavigationController:

// FirstAppAppDelegate.h
@class TableViewController;
@interface FirstAppAppDelegate : NSObject <UIApplicationDelegate>
{
    TableViewController *tableTab; //为了能执行数据保存
}
// FirstAppAppDelegate.m内,不详细写
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
    tableTab = [[TableViewController alloc] init];
    UINavigationController *navTable = [[UINavigationController alloc] initWithRootViewController:tableTab];
// 重复内容...
}

修改TableViewController.h文件,实现NSCoding:

@interface TableViewController : UITableViewController<NSCoding>{
    NSMutableArray *data;
}
@property (nonatomic,retain) NSMutableArray *data;
- (BOOL)saveValue; //保存数据 (data)
- ( void)getValue; //获取数据(data)

取消分组表,使用普通的表样式:

- ( id)init {
    self = [super initWithStyle:UITableViewStylePlain]; // UITableViewStylePlain

//...

}

相应的,取消分组表委托方法:

/* - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     /* if (section==0) {
        return 1;
    }else if(section==1){
        return 2;
    }else{
        return 3;
    }
*/
     return [data count]; //返回数组 的个数
}

 添加获取路径方法:

- (NSString *)pathInDocumentDirectory:(NSString *)fileName
{
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //取当前UserDomain,Document目录。
    NSString *documentDirectory = [documentDirectories objectAtIndex:0]; //多设备兼容,iphone只有一个,取0.
    return [documentDirectory stringByAppendingPathComponent:fileName]; //添加文件名,返回完整目录
}

实现saveValue和getValue方法:

- (BOOL)saveValue
{
     return [NSKeyedArchiver archiveRootObject:data toFile:[self pathInDocumentDirectory: @" data.data "]];//将data存至data.data文件
}
- ( void)getValue
{
     if (!data) {
        data = [[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathInDocumentDirectory: @" data.data "]] retain]; //取回data。自释放,需要添加retain。
    }
     if (!data) {
        data = [[NSMutableArray alloc] init]; //如果不存在文件或者未空,初始化data。
    }
}
NSCoding2个必需实现的委托方法: //固化data
- ( void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:data forKey: @" data "];
}
- ( id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
     if (self) {
        [self setData:[aDecoder decodeObjectForKey: @" data "]];
    }
     return self;
}

 在- viewDidLoad中实现添加nav右按钮和获取data值:

- ( void)viewDidLoad
{
    [super viewDidLoad];
    [self getValue]; //得到data值

    UIBarButtonItem *tableAddBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addData:)];
    [[self navigationItem] setRightBarButtonItem:tableAddBtn];
    [tableAddBtn release];
}

 右按钮的实现:

- ( void)addData:( id)sender
{
     int num = random()% 9; //创建随机数
    NSString *addStr = [NSString stringWithFormat: @" %d ", num]; 
    [data addObject:addStr]; //添加至data
    
    [[self tableView] reloadData]; //重画tableview
}

 最后一步,回到FirstAppAppDelegate.m文件实现应用被挂起后台的操作,添加:

- ( void)applicationDidEnterBackground:(UIApplication *)application
{
    [tableTab saveValue]; //执行保存方法
}

每次关闭App至后台,保存值。打开App,先获取data,不存在则初始化。

 

To 10 code Download:iOS_FirstApp_10.zip

求指点。

转载于:https://www.cnblogs.com/maxfong/archive/2012/05/09/2491672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值