【问题标题】:How to save content of TextField to plist如何将 TextField 的内容保存到 plist
【发布时间】:2013-02-06 05:00:57
【问题描述】:

我想将 TextFields 的内容与相应的键值对保存在 plist 中。 Like Password 字段应与 Key-Password 和 Value-(在 textField 中输入)一起保存。

我该怎么做?

并希望在其他类中访问它。我可以做吗?如果是,那么如何?

【问题讨论】:

    标签: ios objective-c uitextfield ios-simulator plist


    【解决方案1】:

    将东西添加到plist 很容易。完整的工作代码如下,将人员联系信息添加到 plist -

    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
    
    // set the variables to the values in the text fields
    self.personName = nameEntered.text;
    self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
    [phoneNumbers addObject:homePhone.text];
    [phoneNumbers addObject:workPhone.text];
    [phoneNumbers addObject:cellPhone.text];
    
    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];
    
    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
    
    // check is plistData exists
    if(plistData) 
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
        [error release];
    }
    

    [source]

    【讨论】:

    • 嗨,谢谢.. 但我没有得到存储我的 plist 的位置(地点).. 并且我的本地帐户中不存在库.. 该怎么做才能搜索?我也在Finder中搜索过..
    • 我认为它们在 ~/Library/Application Support/iPhone Simulator/User/Applications/ 此处的子文件夹中的某处 - mobiledevelopertips.com/xcode/…
    • 我没有得到库文件夹..它显示了路径,但我无法追踪给定的路径..
    【解决方案2】:

    您可以通过以下方式将textField的内容保存到Plist:

            NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
            [_plistDict setValue:textField.text forKey:@"Password"];
            [_plistDict writeToFile:pListPath atomically:YES];
    

    如果要从 pList 中检索该值,可以使用以下代码:

    NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
        NSString *status = [NSString stringWithFormat:@"%@",[_plistDict objectForKey:@"Password"]];
    

    【讨论】:

      【解决方案3】:

      是的,在 Dictionary 的键中使用 NSMutableDictionaryUITextFeild 属性的值添加到 text 中。喜欢

               NSMutableDicitonary * dictionary = [[NSMutableDictionary alloc] init];
              [dictionary setObject:textfield.text forKey:@"username"];
              [dictionary setObject:textfield.text forKey:@"password"];
      

      将字典保存在 plist 中并在需要的地方使用它。

      【讨论】:

        【解决方案4】:
         NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *aFilePath = [NSString stringWithFormat:@"%@/plistName.plist", aDocumentsDirectory];
        
        NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath];
        
        
        NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
        [newComment setValue:userName.text forKey:@"username"];
        [newComment setValue:password.text forKey:@"password"];
        
        
        [plistArray addObject:newComment];
        [plistArray writeToFile:filePath atomically:YES];
        

        【讨论】:

          【解决方案5】:

          最佳实践将这些信息保存在 NSUserDefaults 而不是 plist 中。可以从项目中的任何位置访问 NSUserDefautls。

          保存:

          NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
          [userDefaults setObject:[yourTextfield text] forKey:@"password"];
          

          检索:

           NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
          

          【讨论】:

          • 这可能是最佳实践,但我们如何才能在其他类中访问 NSUserDefaults?我尝试通过导入类但它显示一些错误..
          • [NSUserDefaults standardUserDefaults] 是单例的,因此可以从应用程序的任何位置访问它。你不必导入任何东西。无需在视图控制器之间传递对象。从一个地方保存并从另一个地方访问。
          • 你只是复制粘贴上面的代码吗?如果是这样,在上面的代码[yourTextfield text],这里yourTextfield应该是你声明的文本字段。在你的代码中改变它。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-11
          • 2013-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多