我现在有一个搜索页面,它将加载网络服务的结果列表,但是当我返回搜索页面时,我想“保存”输入的任何内容(例如“resto italian”),然后将该条目和以前的条目显示到下面的表格视图中,如下图所示:
我的计划是使用属性列表序列化 - 如果还没有列表,则创建一个名为 history.plist 的属性列表,并用所做的每个搜索词填充它,并在上面的表格视图中显示最接近的十个。
我试过的:
// should create history.plist
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingString:@"history.plist"];
}
/* This is the action for when 'search' is clicked - calls the method above to create
a new plist if it's not already created.
I then try to display the contents of the of the file in the textfield itself
(for testing purposes) but it's not saving/displaying properly at the moment. */
- (IBAction)saveHistory:(id)sender {
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
for (int i = 0; i < (sizeof(array)); i++) {
UITextField *theField = self.searchHistory;
theField.text = [NSString stringWithFormat:@"%@", array];
}
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}
任何指向尝试执行此操作的教程的链接、对我应该做什么的建议或对我所拥有的改进的建议将不胜感激。