2012-02-25 66 views
0

在DetailViewController中有一個UITextView,其中的某些文本是從MainViewController的NSMutableDictionary中加載的。這是代碼;在從NSMutableDictionary加載的textview中保存已更改的文本

- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{ 

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 

    if ((int)index == 0) { 
     NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 

         @"This is some text", @"textkey", nil]; 

       detailViewController.myDictionary = myDictionary; 
    } 

我已經加載了這個字符串到我的DetailViewController與此代碼;

self.myText.text = [(NSMutableDictionary *)myDictionary objectForKey:@"textkey"]; 

而且在我viewDidLoad中我創建了一個名爲「保存」,我用它來隱藏鍵盤當觀衆完成編輯RightBarButton。我希望這個按鈕也能保存觀衆進入UITextView(以及原文)的變化。 這是rightBarButton的代碼;

UIBarButtonItem *saveButton =[[UIBarButtonItem alloc] 
            initWithTitle:@"Save" 
            style:UIBarButtonItemStyleBordered 
            target:self 
           action:@selector(textViewDidChange:)]; 

    self.navigationItem.rightBarButtonItem = saveButton; 

最後,我有代碼調用textVidewDidChange並隱藏鍵盤。我正在嘗試保存對textView的更改,但它沒有。

-(void)textViewDidChange:(UITextView *)textView; 
{ 
    if(textView == myText) 
    [myDictionary setValue:myText.text forKey:@"textkey"]; 
    [myText resignFirstResponder]; 
    } 

任何人都可以幫助我做到這一點。我只是想將對UITextView的更改保存回NSMutableDictionary。 (或者也許不那麼簡單)

我已經改變了按鈕 `的UIBarButtonItem * saveButton = [[的UIBarButtonItem頁頭] initWithTitle:@ 「保存」 風格:UIBarButtonItemStyleBordered 目標:自 動作:@selector(didChangeValueForKey :)];

self.navigationItem.rightBarButtonItem = saveButton;` 

等代碼;

` - (void)didChangeValueForKey:(NSString *)key {0} {0} {0} {0} NSError *錯誤;

[myDictionary setValue:[self myText].text forKey:@"textkey"]; 
[myText resignFirstResponder]; 

NSLog(@"%@", [myDictionary valueForKey:@"textkey"]); 

}

我的NSLog顯示了變化,但是當我重新開始他們都走了應用程序,不保存。可以直接保存到NSMutableDictionary嗎? 我讀了很多持久數據。認爲NSData或Plist,但嘗試,因爲我可能做得不好。

有人可以提出一個很好的教程嗎?

我看着建議的鏈接,並將其添加到我的代碼中(粗體部分)。

' - (空)didChangeValueForKey:(的NSString *)鍵{

/* NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:@"textkey"]];*/ 
    NSError *error; 


[myDictionary setValue:[self myText].text forKey:@"textkey"]; 
**[myDictionary writeToFile:@"textkey" atomically:YES];** 

[myText resignFirstResponder]; 

NSLog(@"%@", [myDictionary valueForKey:@"textkey"]); 
}` 

正如你可以看到我也試着讓使用[NSHomeDirector上面,然後更換 @ 「textkey」 與路徑的路徑。我仍然可以看到NSLog中的更改(無論哪種方式),但是當我重新加載視圖或重新啓動應用程序時,沒有任何更改。

我改變了一些事情,以便我在文本視圖中保存文本的文本文件,並從字典中獲取名稱,以便每次加載不同的detailView時,都取決於在mainViewController中選擇的圖像。

這是我在mainViewController中的字典條目;

'如果((int)的指數== 0){ 的NSMutableDictionary * myDictionary =的NSMutableDictionary dictionaryWithObjectsAndKeys: //這裏就是我想要訪問的文本文件名中的UITextView加載在我的DetailView @」首先查看文本」,@ 「textkey2」,

    //This is where I give the text file of the changed text its name for each different view of the DetailView 
        @"first View Text", @"textkey3",nil]; 

      detailViewController.myDictionary = myDictionary; 
}` 

接下來是我用用UIbarbuttonright

'將更改保存到TextView的代碼 - (空)saveAction:(ID)發送{

[self.myText2 resignFirstResponder]; 

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [documentPaths objectAtIndex:0]; 
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[(NSMutableDictionary *)myDictionary objectForKey:@"textkey3"]]; 
NSString *savedString = myText2.text; 

NSLog(@"The Save file is:%@", savedString); 

[savedString writeToFile:documentTXTPath atomically:YES 
       encoding:NSUTF8StringEncoding error:NULL]; 

}`

我已籤文件並將其保存在首先查看文本的名稱下的文件夾,它包含更改的文本。

我在將文本文件內容加載到UITextView時出現問題。 使用代碼我有我得到textkey2對象(第一個文本視圖)的路徑而不是文件的內容。

`NSString *textName = [myDictionary objectForKey:@"textkey2"]; 
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory2 = [paths2 objectAtIndex:0]; 

NSString *fullPath2 = [documentsDirectory2 stringByAppendingPathComponent:textName]; 

/* self.myText2.text = fullPath2; */ self.myText2.text = [NSString的stringWithContentsOfFile:fullPath2編碼:NSUTF8StringEncoding錯誤:NULL];`

我取代這個最後一行與一個沒有被註釋掉的,它工作正常。對於任何想知道的人。

+0

你到底面臨着什麼問題。值不會在字典或其他東西中被替換.... – iphonedev23 2012-02-25 03:48:18

+0

當我向文本視圖添加文本並點擊按鈕時,鍵盤會隱藏,但如果我退出視圖並重新加載它,添加的文本不會出現,因此添加的文本是沒有被保存。 – user1114881 2012-02-25 04:46:02

+1

你確定你的textViewDidChange方法調用? – 2012-02-25 04:56:31

回答

0

您需要實現數據持久性,例如,將文本保存到永久性存儲中,以後重新加載視圖時可以再次檢索該文本。

+0

我添加了一些更改到我的原始帖子的底部,請看。 – user1114881 2012-02-26 23:48:11

+0

我做了一個快速搜索,這是我得到的第一個鏈接:http://stackoverflow.com/questions/7478440/how-to-save-a-nsmutabledictionary-into-a-file-in-documents。這會將字典的內容保存到您可以指定的文件中。然後,您可以在需要時再次加載此特定字典的內容。 – XCool 2012-02-27 14:53:17

+0

看了你的建議後改了一些代碼,仍然沒有去。 – user1114881 2012-02-27 20:08:29