归档TableViewController的data。
为了在Nav上添加Add按钮,为TableViewController创建UINavigationController:
//
FirstAppAppDelegate.h
@class TableViewController;
@interface FirstAppAppDelegate : NSObject <UIApplicationDelegate>
{
TableViewController *tableTab; //为了能执行数据保存
}
@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];
// 重复内容...
}
- (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)
NSMutableArray *data;
}
@property (nonatomic,retain) NSMutableArray *data;
- (BOOL)saveValue; //保存数据 (data)
- ( void)getValue; //获取数据(data)
取消分组表,使用普通的表样式:
- (
id)init {
self = [super initWithStyle:UITableViewStylePlain]; // UITableViewStylePlain
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]; //返回数组 的个数 行
}
{
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]; //添加文件名,返回完整目录
}
{
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。
}
}
{
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;
}
{
[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];
}
{
[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
}
{
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]; //执行保存方法
}
{
[tableTab saveValue]; //执行保存方法
}
每次关闭App至后台,保存值。打开App,先获取data,不存在则初始化。
To 10 code Download:iOS_FirstApp_10.zip
求指点。