好的,我目前有一个主从应用程序,它有一个带有导航控制器的表视图、一个详细视图和一个模式视图。导航视图显示饮品名称,详细视图显示所选饮品的详细信息,模态视图允许我创建新饮品或编辑现有饮品。一切都是从我已经移动到文档目录的 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() 对此进行了测试:一个在我放置在我的 masterViewController 中的 applicationWillTerminate 方法中,一个在我的 AppDelegate.m 中的 applicationWillTerminate 方法中。该日志仅显示在 AppDelegate.m 中的控制台中。所以最后我不知道如何从我的 AppDelegate.m 文件中访问我的 MasterViewController 中的饮料数组,所以我可以使用我的 appDelegate.m 中的 applicationWillTerminate 方法保存到 plist。我将在下面发布我的 appDelegate.m 代码,该代码会引发错误,因为饮料属性位于 MasterViewController 中。
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
}