さて、私は現在、ナビゲーションコントローラー、詳細ビュー、およびモーダルビューを備えたテーブルビューを持つマスター/詳細アプリケーションを持っています。ナビゲーション ビューには飲み物の名前が表示され、詳細ビューには選択した飲み物の詳細が表示され、モーダル ビューでは新しい飲み物を作成したり、既存の飲み物を編集したりできます。すべては、私がすでにドキュメント ディレクトリに移動した plist から取り込まれ、すべてがそこから正常に読み取られています。ビューが空白に変わるかどうかを確認するために空の plist をテストしたので、これが機能することはわかっています。私の見解はすべて完璧に機能します。飲み物の編集と削除を追加し、テーブル ビューを新しい飲み物、削除した飲み物、または変更した飲み物で更新できます。ただし、plist ファイルに正常に書き込みません (masterViewController にある「drinks」という名前の配列から plist を更新しています)。
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [searchPaths objectAtIndex:0];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"];
[self.drinks writeToFile:writeableDBPath atomically:YES];
}
私は 2 つの NSLogs() を配置してこれをテストしました。ログは、AppDelegate.m のログからコンソールにのみ表示されます。最後に、私の MasterViewController にある自分の Drinks 配列に AppDelegate.m ファイルからアクセスする方法がわからないので、appDelegate.m にある applicationWillTerminate メソッドを使用して plist に保存できます。ドリンク プロパティが MasterViewController にあるため、エラーをスローする appDelegate.m コードを以下に投稿します。
AppDelegate.m (メソッド)
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [searchPaths objectAtIndex:0];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"];
[self.drinks writeToFile:writeableDBPath atomically:YES]; //THIS LINE THROWS AN ERROR BECAUSE PROPERTY DRINKS IS ONLY ON MY MASTERVIEWCONTROLLER NOT MY AppDelegate
}