【问题标题】:How to Read and Write a plist that works like iphone note apps如何读写一个像 iphone note 应用程序一样工作的 plist
【发布时间】:2010-12-10 14:31:23
【问题描述】:

您好,我是 iphone 编程新手,我需要一些帮助。

我想创建一个类似记事本的应用程序,使用户能够编写和保存他们的笔记,类似于使用 plist 的 iphone 笔记应用程序,有可能吗?

类似于 iphone 应用程序,我能够列出我创建的所有笔记,这是可能的。

设计对我的应用来说并不重要,因为我只是为了学习。

有没有人可以指导我一个好的教程?

非常感谢你们的回复,任何回复将不胜感激:)

【问题讨论】:

    标签: iphone cocoa cocoa-touch xcode ios4


    【解决方案1】:

    最简单的方法是将文本视图中的文本保存到 .txt 文件中。

    //NotesViewController.h
    
    #import <UIKit/UIKit.h>
    
    
    @interface NotesViewController : UIViewController {
        UITextView *textView;
    }
    @property (nonatomic, retain) UITextView *textView;
    
    -(void)loadNotes;
    -(void)saveNotes;
    
    @end
    
    
    
    //NotesViewContoller.m
    
    #import "NotesViewController.h"
    
    
    @implementation NotesViewController
    
    @synthesize textView;
    
    -(void)viewDidLoad {
         textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
         self.view = textView;
         [self loadNotes];
    }
    
    -(void)loadNotes {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedText.txt"];
        NSString *text = [[NSString alloc] initWithContentsOfFile:path];
        UIColor *clear = [[UIColor alloc] initWithRed:255 green:255 blue:255 alpha:0];
        textView.backgroundColor = clear;
        textView.font = [UIFont fontWithName:@"Arial" size:18.0];
        textView.text = text;
    }
    
    -(void)saveNotes {
        [textView resignFirstResponder];
        NSString *textToSave = [textView text];
    
        if (textToSave == nil || textToSave.length == 0) {
            textToSave = @"";
        }
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedText.txt"];
        [textToSave writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
    }
    

    应该可以,如果不行请告诉我。

    【讨论】:

    • 请注意,现在没有什么可以调用 saveNotes,所以除非你添加一个按钮或调用它的东西,否则你的笔记不会被保存。
    【解决方案2】:

    有可能;)

    存储数据最简单的方法是使用 NSUserDefaults。

    假设你有一个笔记:

    NSString *mynote = [NSString stringWithFormat:@"My first note"];
    

    并希望它保存它,然后你这样做:

    NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
    [userDefs setObject:myNote forKey:@"kMyNote"];
    

    但是您必须为每个音符设置一个键,这是不可取的;)

    所以你可以创建一个 NSArray 并将你的笔记放在那里:

    //inmutable, cannot be changed later
    NSArray *notes = [NSArray arrayWithObjects:myNote, nil];
    
    //or you can try with mutable arrays
    //NSMutableArray *notes = [NSMutableArray array]; 
    //[array addObject:myNote];
    //[array addObject:myNote2];//, add more elements as needed
    
    //save them
    [userDefs setObject:notes forKey:@"kNotes"];
    

    当您想再次获取值时:

    NSArray *savedNotes = [userDefs objectForKey:@"kNotes"];
    

    请注意,您存储的是 NSArray。即使你用“kNotes”存储了一个 NSMutableArray,以后你也会得到一个不可变的 NSArray。

    请进一步阅读NSUserDefaults,因为您可以存储和不能存储的对象类型有一些限制。

    您可以使用这种方法来存储文本本身,也可以像 @Jumhyn 示例那样存储文件的路径。

    如果你不想认真对待这一点,你会发现 CoreData 更加有用和高效;)

    希望对你有帮助

    【讨论】:

    • NSUserDefaults 实际上应该只用于保存的设置。如果有类似文档的数据需要持久化,那么保存到磁盘或 Core Data 会更有效。后两者之一也可以更灵活——例如,您可以启用文档共享以允许用户将笔记复制到他们的计算机上。
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2020-06-10
    • 2013-01-21
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2012-11-07
    相关资源
    最近更新 更多