在从NSMutableDictionary加载的textview中保存已更改的文本
我在DetailViewController中有一个UITextView,其中一些文本是从MainViewController的NSMutableDictionary中加载的。 这是代码;
- (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中,我创建了一个名为'Save'的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 alloc] initWithTitle:@“Save”style:UIBarButtonItemStyleBordered target:self action:@selector(didChangeValueForKey :)];
self.navigationItem.rightBarButtonItem = saveButton;`
和其他代码;
` - (void)didChangeValueForKey:(NSString *)key {NSError * error;
[myDictionary setValue:[self myText].text forKey:@"textkey"];
[myText resignFirstResponder];
NSLog(@"%@", [myDictionary valueForKey:@"textkey"]);
}
我的NSLog显示更改,但是当我重新打开应用程序时,它们已经消失,未保存。 可以直接保存到NSMutableDictionary吗? 我读了很多关于持久数据的内容。 认为NSData或Plist,但尝试,因为我可能做得不好。
有人可以提出一个很好的教程吗?
我查看了建议的链接并将其添加到我的代码中(粗体部分)。
` - (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"]);
}`
正如你所看到的,我也尝试使用上面的[NSHomeDirector]获取路径,然后用路径替换@“textkey”。 我仍然可以看到NSLog中的更改(无论哪种方式),但是当我重新加载视图或重新启动应用程序时,没有任何更改。
我已经改变了一些东西,以便我使用从字典中获取的名称在textview中保存文本的文本文件,以便每次加载不同的detailView时,都取决于在mainViewController中选择的图像。
这是我在mainViewController中的字典条目;
如果((int)索引== 0){NSMutableDictionary * myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys://这里是我想要访问的文本文件名称加载到我的DetailView中的UITextView @“第一视图文本”,@ 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的更改的代码
` - (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];
}`
我已经检查过这个文件,并且该文件保存在第一视图文本名称下的文档文件夹中,并且它包含已更改的文本。
我在将文本文件内容加载到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 encoding:NSUTF8StringEncoding error:NULL];`
我用没有注释掉的最后一行代替,它工作正常。 对于任何想知道的人。
您需要实现数据持久性,例如,将文本保存到永久性存储中,稍后重新加载视图时可以再次检索该文本。
链接地址: http://www.djcxy.com/p/28171.html上一篇: saving changed text in textview loaded from NSMutableDictionary