这是一个简单的项目,我正在从书中复制一半来自学。这是一个待办事项列表,包括表格视图,文本字段和用于将任务添加到列表的按钮。然后它将任务数组保存到目录中(使用辅助函数),当您关闭并重新打开应用程序时,它会检索信息。东西在计算机上运行,我认为它也在设备上工作,直到我关闭应用程序并重新打开它以发现阵列中没有任何元素。因为它在电脑上工作,我不知道什么是不对的。我理解的一些代码,我刚刚复制的一些代码(如帮助函数),任何建议都非常感谢! P
代码:
h file
#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 file
#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。
答案 0 :(得分:0)
我看到需要改变的一件事是你的docPath()
正在回归一条糟糕的道路。您需要使用stringByAppendingPathComponent
代替stringByAppendingString
return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td"];
或者,如果您想使用stringByAppendingString
,那么它应该是:
return [[pathList objectAtIndex:0] stringByAppendingString:@"/data.td"];