ios - 读取和写入 plist(iPhone 编程)

标签 ios objective-c iphone persistence plist

我正在尝试实现“Beginning iPhone 3 Development book”中的一个简单的 plist 示例。我查看了代码,但我的数据从未保存到 plist 文件中。实际上我的项目站点地图如下:每当您启动应用程序时,它都会在 TestViewController 中触发。在 TestViewController 上,有一个按钮。当您单击按钮时,它会推送另一个 View Controller ,即 PersistenceViewController,这是我在 PersistenceViewController 中编写的代码。我的疑问:这个方法中是否调用了 applicationWillTerminate ?我不这么认为..请帮忙。我现在正在学习如何保存数据。

In .h file #define kFilename @"data2.plist"

 - (NSString *)dataFilePath {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:kFilename];
        return path;
    }

    - (void)applicationWillTerminate:(NSNotification *)notification {
        NSMutableArray *contactFormArray = [[NSMutableArray alloc] init];
        NSLog(@"App Terminate:%d",[contactFormArray count]);
        [contactFormArray addObject:nameField.text];
        [contactFormArray addObject:emailField.text];
        [contactFormArray addObject:phoneField.text];
        [contactFormArray addObject:companyField.text];
        [contactFormArray writeToFile:[self dataFilePath] atomically:YES];
        [contactFormArray release];
    }


    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {

        NSString *filePath = [self dataFilePath];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            NSArray *contactFormArray = [[NSArray alloc] initWithContentsOfFile:filePath];
            NSLog(@"Did Load:%d",[contactFormArray count]);
            nameField.text = [contactFormArray objectAtIndex:0];
            emailField.text = [contactFormArray objectAtIndex:1];
            phoneField.text = [contactFormArray objectAtIndex:2];
            companyField.text = [contactFormArray objectAtIndex:3];
            [contactFormArray release];
        }

        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotification object:app];
        [super viewDidLoad];

    }

感谢您提出宝贵的建议...

最佳答案

applicationWillTerminate 在用户退出应用程序时调用(通常通过按 Home 按钮)。该委托(delegate)方法中的任何代码都将被执行(如果不需要太多时间)。在您的情况下,Plist 文件将被保存。

如果您使用的是 iOS 4,按“主页”按钮可能会使您的应用程序进入后台(而不是退出)。如果应用程序被调试器终止或崩溃,则不会调用该方法。

其他信息:

在 iOS 4 上,Xcode 项目中默认启用多任务处理。这可以防止调用 applicationWillTerminate 方法。如果您不想支持多任务处理,请将 UIApplicationExitsOnSuspend 放入 MyAppName-Info.plist 文件中并选中该复选框。如果您确实想支持它,请将您想要在应用程序进入非事件状态之前执行的任何代码放入 applicationDidEnterBackground 委托(delegate)方法中。

关于ios - 读取和写入 plist(iPhone 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4209194/

相关文章:

ios - SwiftUI:在主视图上呈现模态(实际上不是模态) View 时,如何限制 View 的可点击区域?

objective-c - 在IOS中,如何不按顺序播放音频片段的一部分?

iphone - 在 Fox news iPhone 应用程序中自定义 UINavigationBar

iphone - iPhone与Symbian开发投资差异

iphone - 如何计算iPhone ScrollView 的动态大小?

objective-c - 如何在 iOS 中使用 AES128 加密的 Openssl 工具解密数据

ios - 对计算属性的 Realm 结果集合进行排序?

ios - FetchAssetsWithLocalIdentifiers 返回空数组

ios - 如何重置 NSTextStorage 中的属性

ios - 如何在字符串的每一侧 append 一个空格?