2010-12-10 54 views
0

嗨,我是iphone編程的新手,我需要一些幫助。如何讀寫一個像iphone一樣工作的筆記筆記應用

我會想創建一個記事本一樣的應用程序,使用戶編寫和使用的plist,就可以保存自己的筆記,與iPhone類似的應用筆記?

與iPhone類似的應用程序,那我能列出我所創建的所有筆記,這是可能的。

設計對於我的應用程序來說並不重要因爲cos只是爲了我學習。

有沒有人可以指導我一個很好的教程?

我感謝你們在用於回覆的進步,任何答覆將不勝感激:)

回答

1

要做到這一點,最簡單的方法是在你的TextView文本保存爲.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]; 
} 

這應該工作,讓我知道如果它不。

+0

偉大的!謝謝Jumhyn,我會試試看! – Desmond 2010-12-10 06:10:20

+0

請注意,現在沒有什麼可以調用saveNotes,因此除非添加按鈕或調用該按鈕的東西,否則不會保存您的筆記。 – Jumhyn 2010-12-10 06:32:10

0

是可能的;)

用於存儲數據的使用的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"]; 

要知道,你存儲NSArrays。即使你用「kNotes」存儲了一個NSMutableArray,稍後你將得到一個不可變的NSArray。

請,因爲有上那種你可以和不能存儲的對象有一些限制進一步的NSUserDefaults閱讀。

你可以用這種方法來存儲文本本身也許存儲像@Jumhyn示例文件的路徑。

如果你wan't認真對待這一點,你會發現CoreData更加有用和eficient;)

希望它可以幫助

相關問題