【发布时间】:2016-08-10 19:19:11
【问题描述】:
这是一个简单的项目,我从书中复制了一半来自学。这是一个待办事项列表,带有一个表格视图、一个文本字段和一个用于将任务添加到列表中的按钮。然后它将任务数组保存到一个目录(使用帮助函数),当您关闭并重新打开应用程序时,它会检索信息。该东西在计算机上运行,我认为它也可以在设备上运行,直到我关闭应用程序并重新打开它以发现那里没有数组元素。由于它在计算机上工作,我不知道什么是不对的。其中一些代码我理解,一些我现在刚刚复制(如辅助函数),任何建议都非常感谢! P
代码:
h 文件
#import <UIKit/UIKit.h>
NSString *docPath();
@interface ViewController : UIViewController<UITableViewDataSource>
{
UITableView *taskTable;
UITextField *taskField;
UIButton *insertButton;
NSMutableArray *tasks;
}
- (void)addTask:(id)sender;
- (void)writeTasksToFile:(NSString *)path;
@end
m 文件
#import "ViewController.h"
NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathList objectAtIndex:0] stringByAppendingString:@"data.td"];
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
if(plist) {
tasks = [plist mutableCopy];
} else {
tasks = [[NSMutableArray alloc] init];
}
CGRect buttonFrame = CGRectMake(228,40,72,31);
insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[insertButton setFrame:buttonFrame];
insertButton.backgroundColor = [UIColor redColor];
[insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
[insertButton setTitle:@"Insert" forState:UIControlStateNormal];
[[self view] addSubview:insertButton];
CGRect tableFrame = CGRectMake(0, 80, 320, 380);
taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
[taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
taskTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:taskTable];
[taskTable setDataSource:self];
CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
taskField = [[UITextField alloc]initWithFrame:fieldFrame];
[taskField setBorderStyle:UITextBorderStyleRoundedRect];
[taskField setPlaceholder:@"type a task, enter"];
[taskField setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:taskField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void) applicationDidEnterBackground:(UIApplication *)application {
[tasks writeToFile:docPath() atomically:YES];
}
- (void)addTask:(id)sender{
NSString *t = [taskField text];
if ([t isEqualToString:@""]) {return;}
[tasks addObject:t];
[taskTable reloadData];
[taskField setText:@""];
[taskField resignFirstResponder];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tasks count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[tasks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *c = [taskTable dequeueReusableCellWithIdentifier:@"Cell"];
if (!c){
c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];
return c;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)writeTasksToFile:(NSString *)path
{
[tasks writeToFile:path atomically:YES];
}
@end
编辑---------------------------------------------- -------------------------------------------------- ------------------------------
这是一个调试器屏幕截图,在检查是否存在 plist 的行上有一个断点。
【问题讨论】:
-
您是否放置了任何断点以查看发生了什么?也许在'if(plist)'上查看您是否正确加载了文件?另外,你是说你是在 Mac 或 iPhone 上运行它吗?
-
您好,感谢您的回答!我在 Mac 上运行它,当我关闭应用程序(使用 shift 命令 h 两次并向上滑动)并再次重新打开它时,信息仍然存在。在 iPhone 上,信息不存在。至于断点,我在 if(plist) 上放了一个,因为我是新手,我对调试不是很有信心,但我会用它附带的屏幕截图来编辑问题。
-
我已按照您的建议在检查“if(plist)”的行上放置了三个中断点,然后在下一行和下一行放置一个。当我在 mac 上运行它时,plist 在第一次休息时有 4 个元素和任务 nil,然后在第二次休息时它们都有 4 个元素(这是可以预期的,因为它已经完成了一个可变副本)。在设备上,添加要做的事情并重新启动后,plist 有 nil 元素。
标签: ios objective-c save device nsdocumentdirectory