Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 读写plist(iPhone编程)_Ios_Objective C_Iphone_Persistence_Plist - Fatal编程技术网

Ios 读写plist(iPhone编程)

Ios 读写plist(iPhone编程),ios,objective-c,iphone,persistence,plist,Ios,Objective C,Iphone,Persistence,Plist,我正试图实现一个简单的plist示例,它来自“开始iPhone3开发书”。我查看了代码,但我的数据从未保存到plist文件中。事实上,我的项目站点图如下:无论何时启动应用程序,它都会在TestViewController中启动。在TestViewController上,有一个按钮。当您单击按钮时,它会按下另一个视图控制器,即PersistenceViewController,下面是我在PersistenceViewController中编写的代码。我的疑问是:应用程序是否将终止在此方法中的调用?

我正试图实现一个简单的plist示例,它来自“开始iPhone3开发书”。我查看了代码,但我的数据从未保存到plist文件中。事实上,我的项目站点图如下:无论何时启动应用程序,它都会在TestViewController中启动。在TestViewController上,有一个按钮。当您单击按钮时,它会按下另一个视图控制器,即PersistenceViewController,下面是我在PersistenceViewController中编写的代码。我的疑问是:应用程序是否将终止在此方法中的调用?我不这么认为……请帮忙。我现在正在学习如何保存数据

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];

    }

感谢您提供的宝贵建议…

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

如果您使用的是iOS 4,按Home按钮可能会将您的应用程序发送到后台(而不是退出)。如果应用程序使用调试器被终止或崩溃,则不会调用该方法

其他信息:


在iOS 4上,默认情况下在Xcode项目中启用多任务处理。这将防止调用
应用程序willterminate
方法。如果不想支持多任务处理,请将
UIApplicationExitsOnSuspend
放在MyAppName-Info.plist文件中,并选中复选框。如果您确实希望支持它,请在应用程序进入非活动状态之前,将要执行的任何代码放入
ApplicationIdentinterBackground
委托方法中。

如果应用程序被挂起,然后通过多任务UI终止,则不会在多任务设备(iOS4)上调用应用程序。苹果称:“应用程序不知道任何进入或退出暂停状态的转换”。因此,如果要在applicationWillTerminate中保存任何内容,请尝试在applicationWillResignActive:中执行此操作。

我可能在这里弄错了,但applicationWillTerminate是否必须编码到应用程序的delegate.m文件中,而不是其他.m文件中?无论如何,这可能并不重要,因为iOS 4中的应用程序处于暂停状态。

Evan..谢谢你的评论..是的,我正在使用iOS 4。我看不到在documents文件夹中创建的任何plist文件,甚至我试图创建一个plist文件并将其放置在documents文件夹中,但当我点击Home按钮时,内容没有保存。当我重新启动应用程序时,它会显示空表单。你能帮我或指导我解决这个问题吗?…谢谢你的建议。金如…埃文的建议对我有用:)