saving changed text in textview loaded from NSMutableDictionary

I have a UITextView in a DetailViewController with some text which is loaded from a NSMutableDictionary in the MainViewController. Here is the code for that;

- (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;
    }

I have loaded the string from this into my DetailViewController with this code;

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

Also in my viewDidLoad I have created a RightBarButton named 'Save' which I use to hide the keyboard when the viewer is done editing. I would like this button to also save the changes the viewer enters into the UITextView (as well as the original text). This is the code for the rightBarButton;

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

    self.navigationItem.rightBarButtonItem = saveButton; 

Lastly I have code which envokes the textVidewDidChange and hides the keyboard. I am trying to also have it save the changes to the textView but it doesn't.

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

Can anyone help me accomplish this. I simply want to save the changes to the UITextView back to the NSMutableDictionary. (or maybe not so simply)

I have changed the button to `UIBarButtonItem *saveButton =[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleBordered target:self action:@selector(didChangeValueForKey:)];

self.navigationItem.rightBarButtonItem = saveButton;`

and other code to ;

`-(void)didChangeValueForKey:(NSString *)key{ NSError *error;

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

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

}

My NSLog shows the changes but when I reopen the app they are gone, not saved. Can one save directly to the NSMutableDictionary? I read a lot on persistent data. Thought NSData or Plist but try as I may not doing well.

Can someone suggest a good tutorial on this?

I looked at suggested link and added this (bold part) to my code.

`-(void)didChangeValueForKey:(NSString *)key{

/* 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"]);
}`

As you can see I also tried getting the path using [NSHomeDirector] above and then replaced @"textkey" with path. I can still see the changes in NSLog (either way) but there is no change when I reload the view or relaunch the app.

I have change things so that I am saving a text file of the text in the textview with the name gotten from the dictionary so that each time a different detailView is loaded depending on the image selected in the mainViewController.

This is my dictionary entry in the mainViewController;

`if ((int)index == 0) { NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: //here is where I want to access the text file name to load in the UITextView in my DetailView @"first View Text", @"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;
}`

Next is the code I use to save the changes to the textView using the UIbarbuttonright

`- (void)saveAction:(id)sender {

[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];

}`

I have checked this and the file is saved in the documents folder under the name of First View Text and it does contain the changed text.

I am having problems loading the text file contents into the UITextView. Using the code I have I get the path to the textkey2 object (First Text View) not the contents of the file.

`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 encoding:NSUTF8StringEncoding error:NULL];`

I replaced this last line with the one that isn't commented out and it works fine. For anyone who want to know.


您需要实现数据持久性,例如,将文本保存到永久性存储中,稍后重新加载视图时可以再次检索该文本。

链接地址: http://www.djcxy.com/p/28172.html

上一篇: UIImageView需要时间加载图像

下一篇: 在从NSMutableDictionary加载的textview中保存已更改的文本