PDA

View Full Version : saving textfield text as a .txt file from NSdictionary key




gbenna
Mar 4, 2012, 12:49 AM
I am trying to save the text entered into a UITextField in a DetailView as a .txt file into the Documents Directory. I get the name of the .txt file from an NSMutableDictionary in a mainView which is a TableView.

Here is my code in the MainView;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
const NSDictionary *const row = [self rowForIndexPath:indexPath];

NSString *wantedClassName = [row objectForKey:@"controller"];

UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
NSLog(@"controller is -%@-", wantedClassName);


if ([vc isKindOfClass:[DetailViewController class]])
{
DetailViewController *DetailView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
**//This is where I think my problem is**
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"image.jpg",@"photokey",
@"name.txt", @"identity", nil];

DetailView.myDictionary = myDictionary;
}
[self.navigationController pushViewController:vc animated:YES];
}

In my DetailViewController I have this code to save the text;

-(IBAction)saveText:(id)sender

{
[self.name resignFirstResponder];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[(NSMutableDictionary *)familyDictionary objectForKey:@"identity"]];
NSString *savedString = name.text;

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

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


I can input the text into the textfield and hit the button. The log shows the text being entered but I think I am not getting the 'key' from the dictionary the text can't be written to a txt file. It doesn't show up in the Document's Directory. I know that I could save this to NSUserDefault but I have my reasons why I don't want to. Can someone help me figure out how to access the Dictionary key?

Thanks



dejo
Mar 4, 2012, 03:11 AM
What is the difference between DetailViewController's myDictionary property and familyDictionary?

PhoneyDeveloper
Mar 4, 2012, 10:48 AM
writetToFile returns a BOOL and also an NSError* when it fails. You should inspect those.

Why does your didSelectRowAtIndexPath: method create two view controllers and abandon one of them?

gbenna
Mar 4, 2012, 11:54 PM
there should be and now isn't any difference between familyDictionary and myDictionary. It was a typo.

I see what you mean about the view controller.

I want to use the wanted view controller because then I can choose from different view controllers depending on the cell selected from a plist. So I have changed the code to this.


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
const NSDictionary *const row = [self rowForIndexPath:indexPath];

NSString *wantedClassName = [row objectForKey:@"controller"];

UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
NSLog(@"controller is -%@-", wantedClassName);



if ([vc isKindOfClass:[detailViewController class]])
{
detailViewController*lcVC =(detailViewController*)vc;

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:

@"image.jpg",@"photokey",
@"name.txt", @"identity",nil];
lcVC.myDictionary = myDictionary;


NSLog(@"numberkey");

}
[self.navigationController pushViewController:vc animated:YES];

}

I don't get any errors or warnings with this and logs show the text I am inputting in the simulator. But I don't get anything from the NSLog(@"number key"); above.

thanks for the help.

gbenna
Mar 5, 2012, 01:11 AM
Sorry stupid me I had put the NSMutableDictionary into the wrong ViewController tableview. DOH!!! that part works fine now but I'm sure I have other legit questions.