現時点では、Web サービスの結果のリストをロードする検索ページがありますが、検索ページに戻ったときに、入力した内容 (「resto italian」など)を「保存」したいと思います。次の画像のように、そのエントリと前のエントリを下のテーブル ビューに表示します。
私の計画は、プロパティ リストのシリアル化を使用することでした。リストがまだない場合は、history.plist という名前のプロパティ リストを作成し、作成された各検索語を入力して、上記のようにテーブル ビューに最も近い 10 個を表示します。
私が試したこと:
// 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];
}
これを試みているチュートリアルへのリンク、何をすべきかについての提案、または私が持っているものの改善は大歓迎です。