Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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
C++ 退出应用程序时保存整数-苹果iOS_C++_Ios_Save_Quit - Fatal编程技术网

C++ 退出应用程序时保存整数-苹果iOS

C++ 退出应用程序时保存整数-苹果iOS,c++,ios,save,quit,C++,Ios,Save,Quit,右我得到了下面的代码,它将保存一个整数时,保存按钮被点击,应用程序被关闭,但我需要它自动做到这一点。。。我如何在ios上实现这一点?对于整个苹果编码来说都是全新的,所以欢迎任何帮助 -(IBAction)SaveNumber:(id)sender{ [[NSUserDefaults standardUserDefaults] setInteger:Number forKey:@"savenumber"]; } - (void)viewDidLoad { Number = [[

右我得到了下面的代码,它将保存一个整数时,保存按钮被点击,应用程序被关闭,但我需要它自动做到这一点。。。我如何在ios上实现这一点?对于整个苹果编码来说都是全新的,所以欢迎任何帮助

-(IBAction)SaveNumber:(id)sender{
    [[NSUserDefaults standardUserDefaults] setInteger:Number forKey:@"savenumber"];
}

- (void)viewDidLoad {

    Number = [[NSUserDefaults standardUserDefaults] integerForKey:@"savenumber"];
    ShowSavedNumber.Text = [NSString stringWithFormat:@"%i",Number];

    [super viewDidLoad];
}

对NSUserDefault所做的更改不会自动保存。您必须在NSUserDefaults对象上调用-synchronize才能保存。通常,您希望在完成计划执行的所有更新后执行一次(如果实际上需要执行多个更新)


这是苹果公司的参考资料:

问题是,你在哪里保存这个值?您没有文件可将此值存储在应用程序内的Documents目录中

您可以在应用程序中保存任何值,但必须使用文件进行存储和读取,或通过服务器上的URL进行保存和加载。比如file.plist、SqlLight、json和MySql

因此,第一个简单的保存方法是在应用程序中的AppDelegate.m下使用一个文件
.plist
,您必须检查您的目录是否存在该文件,并将其复制到Documents目录(唯一可写的文件夹)中,但之后您必须学习如何使用
字典
,在同一文件上加载和保存,
MutableDictionary
和其他一些类。我可以帮助您理解第一步,比如检查并创建一个.plist、read和first base来保存,但另一方面,代码太多,yopu必须观看其他教程

因此,创建一个.plist文件并更改结构,如下所示: Change键有您想要的,但是请记住字典包含更多的字符串值,如果您想要添加更多的项,您必须在同一根数组中添加其他行

Root = Array
Item0 > some string with value
Item1 > some string with value
现在,当应用程序启动时,您必须将此文件复制到可写文档文件夹中,因此在
AppDelegate.m
中创建一个函数:

- (void)createPlistFile{

    NSError *error;
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourFile.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"plist"];

        [fileManager copyItemAtPath:bundle toPath: path error:&error];
    }

}
-(BOOL)应用程序下:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
添加调用:
[self-createPlistFile];

好的,现在您必须首先检查文件是否存在于documents文件夹中,如果为false,应用程序将创建一个新文件

下面是读取应用程序中新文件的标准代码:

ViewControlle.h
中写入:

@interface ViewController : UIViewController {
    NSMutableArray *loadData;
}

@property (nonatomic, strong) NSMutableArray *loadData;
回到您的
ViewController.m
并写下:

@synthesize loadData;

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"yourFile.plist"];
    NSMutableArray *dataDict = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    self.loadData = dataDict;
好的,现在你已经在一个文档目录中创建了一个新的PLIST文件并正确加载了,但是现在你必须保存,这是一个基本的保存方法,但是有很多种模式,我不能全部写在这里;)


好的,就这些,我希望这能帮助你开始理解如何在应用程序上保存数据如果是,请投票;)

我猜你只想在
-(void)applicationIdentinterbackground:(UIApplication*)应用程序中的AppDelegate中添加这段代码。然后它会“自动”保存你想要的任何东西没有任何用户输入。这让我很困惑:(他说的是在退出应用程序之前调用
[[NSUserDefaults standardUserDefaults]synchronize];
来同步您的用户默认值。
- (IBAction)save:(id)sender {

    NSMutableDictionary *nr2 = [[NSMutableDictionary alloc] init];
    [nr2 setValue:emittente.text forKey:@"plistKey"];
    [nr2 setValue:link.text forKey:@"plistKey"];
    [nr2 setValue:images.text forKey:@"plistKey"];
    [nr2 setValue:web.text forKey:@"plistKey"];

    [YouMutableDict addObject:nr2];
    [YouMutableDict writeToFile:listFav atomically:YES];
}