zoukankan      html  css  js  c++  java
  • iphone开发中的数据存储:Property lists

    定义两个方法:

    - (NSString *)dataFilePath;获取返回数据文件的完整路径名

    - (void)applicationWillResignActive:(NSNotification *)notification;保存数据,参数notification的作用是在对象之间传递通知,保持通信。

    #define kFilename        @"data.plist"


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

     

    - (void)applicationWillResignActive:(NSNotification *)notification {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:field1.text];
    [array addObject:field2.text];
    [array addObject:field3.text];
    [array addObject:field4.text];
    [array writeToFile:[self dataFilePath] atomically:YES];
    }

    (例子程序为保存四个textField的字符)

    之后在viewDidLoad中添加代码:

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    field1.text = [array objectAtIndex:0];
    field2.text = [array objectAtIndex:1];
    field3.text = [array objectAtIndex:2];
    field4.text = [array objectAtIndex:3];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(applicationWillResignActive:)
    name:UIApplicationWillResignActiveNotification
    object:app];
    }




  • 相关阅读:
    Qt Quick实现的涂鸦程序
    Java并发学习之十九——线程同步工具之Phaser
    poj 1845(等比数列前n项和及高速幂)
    装饰模式(旧恋)
    cocos2d-x 3.1.1 学习笔记[3]Action 动作
    Nmap 源代码学习四 软件简单使用
    关于phpcmsv9更新缓存出现链接被重置的问题
    POJ 3159 Candies(SPFA+栈)差分约束
    Ubuntu 配置ISCSI服务
    iSCSI存储技术
  • 原文地址:https://www.cnblogs.com/mybkn/p/2418996.html
Copyright © 2011-2022 走看看