嘿大家。我正在开发一个在消费者第一次使用时显示模式的应用程序。我有一个 .plist 文件,一旦按下 Save 按钮,它将存储我请求的信息。我可以很好地读取 .plist 文件,当我运行我的保存方法时,它似乎可以正常工作,但 .plist 文件没有更新。我不太确定这里的问题。我展示了这样的模态。
- (void) getConsumerInfo {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
NSMutableDictionary *plistConsumerInfo = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *hasAppeared = [plistConsumerInfo objectForKey:@"HasAppeared"];
if(hasAppeared != kHasAppeared) {
ConsumerInfoViewController *tmpConsumerInfoVC = [[ConsumerInfoViewController alloc]
initWithNibName:@"ConsumerInfoView" bundle:nil];
self.consumerInfoViewController = tmpConsumerInfoVC;
[self presentModalViewController:consumerInfoViewController animated:YES];
[tmpConsumerInfoVC release];
}
}
这在应用程序启动时由第一个视图手机中的 viewDidLoad 方法调用。在 consumerInfoViewController 中,我有输入数据的文本字段,当按下 Save 按钮时,它会调用此方法。
- (IBAction)saveConsumerInfo:(id)sender {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSString *tmpDiversName = txtDiversName.text;
NSString *tmpLicenseType = txtLicenseType.text;
NSString *tmpLicenseNum = txtLicenseNumber.text;
NSString *tmpHasAppeared = @"1";
NSString *tmpNumJumps = @"3";
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpDiversName] forKey:@"ConsumerName"];
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseType] forKey:@"LicenseType"];
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseNum] forKey:@"LicenseNumb"];
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpNumJumps] forKey:@"NumJumps"];
[plistDict setValue:[[NSString alloc] initWithFormat:@"%d", tmpHasAppeared] forKey:@"Show"];
[plistDict writeToFile:filePath atomically: YES];
NSLog([[NSString alloc] initWithContentsOfFile:filePath]);
[self dismissModalViewControllerAnimated:YES];
}
这一切都运行良好,没有打嗝,但文件永远不会更新。我希望它更新,以便我可以在整个应用程序中使用此信息并更新标志,以便在输入数据后它不会再次显示视图。如果您需要更多信息,请告诉我。提前致谢!